C++程序找出至少需要多少分数才能获得G分


假设我们有两个数组p和c,它们都包含D个元素,还有一个数字G。考虑在一个编码竞赛中,每个问题的得分都基于难度。问题p[i]的得分为100i。这些p[1] + ... + p[D]个问题是竞赛中存在的所有问题。编码网站中的用户有一个分数total_score。用户的total_score是以下两个元素的总和。

  • 基础分数:所有已解决问题的分数之和

  • 奖励:当用户解决所有得分为100i的问题时,除了基础分数外,他/她还会获得完美的奖励c[i]。

Amal是竞赛新手,还没有解决任何问题。他的目标是获得G分或更高的分数。我们必须找到为了达到这个目标,他至少需要解决多少个问题。

因此,如果输入类似于G = 500;P = [3, 5];C = [500, 800],则输出将为3

步骤

为了解决这个问题,我们将遵循以下步骤:

D := size of p
mi := 10000
for initialize i := 0, when i < 1 << D, update (increase i by 1), do:
sum := 0
count := 0
at := 0
an array to store 10 bits b, initialize from bit value of i
for initialize j := 0, when j < D, update (increase j by 1), do:
   if jth bit in b is 1, then:
      count := p[j]
      sum := sum + ((j + 1) * 100 * p[j] + c[j]
   Otherwise
      at := j
if sum < G, then:
   d := (G - sum + (at + 1) * 100 - 1) / ((at + 1) * 100)
   if d <= p[at], then:
      sum := sum + (at + 1)
      count := count + d
if sum >= G, then:
   mi := minimum of mi and count
return mi

示例

让我们看看以下实现,以便更好地理解:

Open Compiler
#include <bits/stdc++.h> using namespace std; int solve(int G, vector<int> p, vector<int> c){ int D = p.size(); int mi = 10000; for (int i = 0; i < 1 << D; i++){ int sum = 0; int count = 0; int at = 0; bitset<10> b(i); for (int j = 0; j < D; j++){ if (b.test(j)){ count += p.at(j); sum += (j + 1) * 100 * p.at(j) + c.at(j); } else { at = j; } } if (sum < G){ int d = (G - sum + (at + 1) * 100 - 1) / ((at + 1) * 100); if (d <= p.at(at)){ sum += (at + 1) * 100 * d; count += d; } } if (sum >= G) { mi = min(mi, count); } } return mi; } int main() { int G = 500; vector<int> P = { 3, 5 }; vector<int> C = { 500, 800 }; cout << solve(G, P, C) << endl; }

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输入

500, { 3, 5 }, { 500, 800 }

输出

3

更新于: 2022年3月2日

237 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告