使用C++的CCF-CSP满分解决方案 202012-2 期末预测之最佳阈值

这段代码展示了一个利用前缀和与后缀和优化的算法来处理评分系统的策略。通过遍历输入的分数和失败状态,分别计算成功和失败的累计次数,然后找到最优的分数阈值,使得成功预测数最多。该方法提高了效率,避免了简单遍历的局限。

在这里插入图片描述
同样,用最简单的遍历还是只能拿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;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值