
同样,用最简单的遍历还是只能拿70分,想要拿满得用前缀和、后缀和
代码实现
#include <map>
#include <set>
#include <queue>
#include <iostream>
#include <algorithm>
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;
}
这段代码展示了一个利用前缀和与后缀和优化的算法来处理评分系统的策略。通过遍历输入的分数和失败状态,分别计算成功和失败的累计次数,然后找到最优的分数阈值,使得成功预测数最多。该方法提高了效率,避免了简单遍历的局限。
700

被折叠的 条评论
为什么被折叠?



