I was trying to solve this problem of codeforces and i ended up very frustrated. I have a solution, and when i try it on the online judge he says me that it didn't work. I have seen it several times in order to find the problem but i definitely can't found it. The strategy is the following:
For each cash withdrawal (from the big one to the little):
- Calculate the number of bills needed for the actual bill denomination. And keep on doing this with the for-bucle.
- If a denomination was used then increase in one the counter.
- Calculate the rest of money using the "module".
Finally, i check the conditions of the problem using a if. The conditions are: not using more than k bills, withdrawal all the mone and don't use more than 2 different denominations.
This is the link for the problem statement and here is my code in C++11.
Link: http://codeforces.com/problemset/problem/524/C
#include <iostream>
using namespace std;
int main(){
long long sol,counter;
int q,n,k;
cin >> n;
cin >> k;
long long a[n]; // types of denominations
for(int i = 0; i < n; i++){
cin >> a[i];
}
cin >> q;
long long b[q]; // cash to withdrawal
for(int i = 0; i < q; i++){
cin >> b[i];
sol = 0;
counter = 0;
for(int j = n-1; j >= 0; j--){
sol += b[i]/a[j];
counter += (b[i]/a[j]!=0);
b[i] = b[i]%a[j];
}
if(sol <= k && b[i] == 0 && counter <= 2){
cout << sol << endl;
} else {
cout << -1 << endl;
}
}
}
PD: I am not a native english speaking so sorry if something can't be understood. PD2: I am also begining with C++11 and this kind of problems.
Very thank for your help. I am waiting for some response.
Aucun commentaire:
Enregistrer un commentaire