I was working on this problem:
http://www.usaco.org/index.php?page=viewproblem2&cpid=763
And this is my code
#include <iostream>
#include <map>
#include <set>
#include <unordered_set>
#include <vector>
using namespace std;
//method to caluclate the best cows
unordered_set<long long> getBestCows(map<long long,long long>& cows){
unordered_set<long long> bestCows;
map<long long,long long>::iterator it;
long long maxValue=-2147483647;
for(it = cows.begin();it!=cows.end();it++){
maxValue=max(maxValue,it->second);
}
map<long long,long long>::iterator it2;
for(it2 = cows.begin();it2!=cows.end();it2++){
if(it2->second==maxValue){
bestCows.insert(it2->first);
}
}
return bestCows;
}
int main() {
// freopen("measurement.in", "r", stdin);
// freopen("measurement.out", "w", stdout);
//read inputs
long long N,G;
cin>>N>>G;
map<long long,pair<long long,long long>> days;
unordered_set<long long> bestCows;
map<long long,long long> cows;
for(long long i=0;i<N;i++){
long long input1,input2,input3;
cin>>input1>>input2>>input3;
days.insert({input1,{input2,input3}});
cows.insert({input2,G});
bestCows.insert(input2);
}
//input reading done
long long ans=0;
map<long long,pair<long long,long long>>::iterator it;
long long curNumBest=-2147483647;
for(it=days.begin();it!=days.end();it++){
cows[it->second.first]+=it->second.second;
//I know that this part is wrong but i dont know why. I need this because it makes it much more efficent
if(cows[it->second.first]<curNumBest && bestCows.find(it->second.first)==bestCows.end()){
continue;
}
//
unordered_set<long long> cur=getBestCows(cows);
//check if it changed
if(cur!=bestCows){
ans++;
curNumBest=cows[*cur.begin()];
bestCows=cur;
}
}
cout<<ans;
return 0;
}
I marked the part which I know is wrong in the comments. I don’t know why it is wrong though. When I remove it gives the right answer for all test cases, but it is slow. When I add it, it works on everything except for tc 6 and 9.
these are the results:
Can someone tell me why that part is wrong?