【问题标题】:INOI 2011 Question 2: IOI Training Camp 20xx (Python)INOI 2011 问题 2:IOI 训练营 20xx (Python)
【发布时间】:2020-03-26 19:23:28
【问题描述】:

谁能帮我解答这个问题? 我需要它的python解决方案

我们已经进入了 21 世纪,小学生在第 4 班学习动态编程。IOI 训练营已经退化为无休止的测试序列,分数为负。在训练营结束时,每个学生都将根据整个测试序列中最好的连续部分(即没有差距)的总和进行评估。

然而,学生们多年来并没有太大变化,他们要求在评估程序中放宽一些。作为一项让步,营地协调员已同意允许学生在计算最佳成绩时放弃一定数量的测试。

例如,假设Lavanya是训练营的一名学生,已经进行了十次测试,她的分数如下。

测试 1 2 3 4 5 6 7 8 9 10

标记 6 -5 3 -7 6 -1 10 -8 -8 8

在这种情况下,不允许放弃任何测试,最好的部分是测试 5-7,总共产生 15 分。如果允许 Lavanya 在一个部分中最多放弃 2 个测试,那么最好的部分是测试 1-7,在放弃测试 2 和 4 后总共会产生 24 分。如果允许她在一个部分中最多放弃 6 个测试,则最好的总数是通过取出整个列表并删除 5 个否定条目得到总共 33 个。

您将获得一个包含 N 个测试标记的序列和一个数字 K。当最多 K 个标记可能从该片段中删除时,您必须计算该序列中最佳片段的总和。

【问题讨论】:

    标签: python dynamic-programming


    【解决方案1】:

    我希望这会有所帮助:

    # Read N and K
    (N, K) = input().strip().split()
    (N, K) = (int(N), int(K))
    # Read Marks
    marks = []
    for i in range(N):
      x = int(input())
      marks.append(x)
    #Let Best[i][j] denote the maximum segment ending at position i with at most j marks dropped.
    Best = [[0 for i in range(K + 1)] for j in range(len(marks))] 
    Best[0][0] = marks[0]
    #Filling up Best[i][j] for j = 0, i.e 1 mark dropped.
    for i in range(1, len(marks)):
      if marks[i] < (Best[i - 1][0] + marks[i]):
        Best[i][0] = Best[i - 1][0] + marks[i]
      else: 
        Best[i][0] = marks[i]
    #Inductively filling the rest of the list(Best)
    for j in range(1, K + 1):
      for i in range(len(marks)):
        Best[i][j] = max(Best[i - 1][j - 1], marks[i] + Best[i - 1][j], Best[i][j - 1])
    #Finding the maximum
    maxMarks = Best[0][K]
    for i in range(len(marks)):
      if Best[i][K] > maxMarks:
        maxMarks = Best[i][K]
    print(maxMarks)
    

    【讨论】:

      【解决方案2】:
      (Nstr,Kstr) = input().strip().split()
      (N,K) = (int(Nstr),int(Kstr))
      
      # Read marks
      
      marks = [ 0 for i in range(N) ]
      for i in range(N):
        marks[i] = int(input().strip())
      
      # Initialize best
      
      best = [ [ 0 for j in range(K+1) ] for i in range(N) ]
      
      # Base case, incrementally track best answer
      best[0][0] = marks[0]
      ans = marks[0]
      
      for j in range(1,K+1):
         best[0][j] = max(marks[0],0)
         ans = max(ans,best[0][j])
      
      # Inductive case
      for i in range(1,N):
      
        # Normal maximum segment
        best[i][0] = max(best[i-1][0],0)+marks[i]
        ans = max(ans,best[i][0])
      
        # Maximum segment with dropping
        for j in range(1,K+1):
          best[i][j] = max(best[i-1][j]+marks[i],best[i-1][j-1])
          ans = max(ans,best[i][j])
      
      # Final answer
      print(ans)       
      

      【讨论】:

      • 感谢您抽出宝贵时间提供答案。如果我们对您的代码添加一些解释,那就更好了。
      【解决方案3】:
      N,K = list(map(int,input().split()))
      final = [0]
      output = 0
      for i in range(N):
          final.    append(  int    (input()))
      score = [[0 for i in range(K+1)] for j in range(N+1)]
      for i in range(1, N+1):
          score[i][0] = max(score[i-1][0]+final[i], final[i])
          for j in range(1, min(i+1, K+1)):
              score[i][j] = max(score[i-1][j]+final[i], score[i-1][j-1])
      for i in range(1, N+1):
          output = max(output, score[i][K])
      print(output)
      

      【讨论】:

      • 感谢您抽出宝贵时间提供答案。如果我们对您的代码添加一些解释,那就更好了。
      猜你喜欢
      • 2021-10-19
      • 1970-01-01
      • 2013-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-05
      • 2015-11-08
      • 1970-01-01
      相关资源
      最近更新 更多