Home  >  Article  >  Backend Development  >  C++ program to find out the minimum number of points needed to achieve a G score

C++ program to find out the minimum number of points needed to achieve a G score

WBOY
WBOYforward
2023-09-06 21:25:06803browse

C++ program to find out the minimum number of points needed to achieve a G score

Suppose we have two arrays p and c, each array has D elements, and there is another number G. Consider that in a programming competition, each question is scored based on its difficulty. The score of question p[i] is 100i. These p[1]...p[D] problems are all problems in the competition. Users on programming websites have a numerical total_score. The user's total_score is the sum of the following two elements.

  • Basic score: The sum of the scores of all problems solved

  • Reward: When When users solve all problems with a score of 100i, in addition to the basic score, they will also receive a perfect reward c[i].

Amal is new to the competition and has not solved any problems yet. His goal is to get an overall grade of G or more. We need to find out how many problems he needs to solve at least to reach this goal.

So if the input is G = 500; P = [3, 5]; C = [500, 800], then the output will be 3

Steps

For To solve this problem, we will follow the following steps:

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

Example

Let us see the implementation below for better understanding −

#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;
}

Input

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

Output

3

The above is the detailed content of C++ program to find out the minimum number of points needed to achieve a G score. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete