Ich habe einen Code, wo ich eine Karte rückwärts in C++ iterieren. Bei n Paaren in der Karte werden manchmal n Paare, manchmal n-1 Paare gedruckt. für Testfallumgekehrte Iteration der Karte in C++ fehlt das erste Element
4 2
2 1
3 5
2 3
1 5
druckt 4 hallo ist, für die
4 1
0 2
0 3
3 5
0 6
Mein Code nächsten Fall 3 hallo:
include cmath, cstdio, vector, iostream, map, algorithm, using namespace std;
int main() {
int n,f;
cin >> n >> f;
int k[n],l[n],dif[n];
map<int,int> m;
map<int,int>::reverse_iterator it;
for (int i=0;i<n;i++){
scanf("%d %d\n",&k[i],&l[i]);
dif[i] = l[i]-k[i];
m.insert(make_pair(dif[i],l[i]));
}
int a = 0;
int sum = 0;
for(it = m.rbegin(); it != m.rend(); ++it) {
cout << "hi" << endl;
int article = it->second - it->first;
int people = it->second;
if (a < f and article!=0){
a++;
if (2*article > people){
sum = sum + people;
} else {
sum = sum + (2*article);
}
} else {
if (article > people){
sum = sum + people;
} else {
sum = sum + article;
}
}
}
cout << sum;
return 0;
}
Sie möchten wahrscheinlich eine 'multimap' anstatt einer' map'. – Bernard