
同样,用最简单的遍历还是只能拿70分,想要拿满得用前缀和、后缀和
#include
#include
#include
#include
#include
using namespace std;
int main() {
// 提高cin,cout的速度
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
map<int, int> succeed, fail, total_succeed, total_fail;
set<int> scores;
int n;
cin >> n;
int score;
bool not_failed;
while (n--) {
cin >> score >> not_failed;
if (not_failed) succeed[score]++;
else fail[score]++;
scores.emplace(score);
}
int pre = 0;
for (auto it = scores.rbegin(); it != scores.rend(); it++) {
total_succeed[*it] = succeed[*it] + pre;
pre += succeed[*it];
}
pre = 0;
for (auto it = scores.begin(); it != scores.end(); it++) {
total_fail[*it] = fail[*it] + pre;
pre += fail[*it];
}
int best = 0, curr_predict = 0, best_predict = 0;
for (auto s : scores) {
curr_predict = total_succeed[s] + total_fail[s] - fail[s];
if (curr_predict >= best_predict) {
best = s;
best_predict = curr_predict;
}
}
cout << best;
return 0;
}