
从最高分开始向下遍历,当分数线取最高分时,正确个数为所有分没过的个数加上最高分通过的个数,再向下取分数线,修改预测正确个数。
#include<iostream>
#include<algorithm>
#define maxn 100002
using namespace std;
struct myclass{//成绩,通过或未通过
int grade;
short fail = 0;
short pass = 0;
};
short d[maxn] = {0};
bool cmp(struct myclass a, struct myclass b) {
return a.grade < b.grade;
}
struct myclass t[maxn];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
if(b==1){
t[i].grade = a;
t[i].pass = 1;
}
else {
t[i].grade = a;
t[i].fail = 1;
}
}
sort(t, t + n, cmp);//按成绩从低到高排序
int head, tail;
head = 0;
tail = 1;
int pre;
while (tail<n)//统计每个分数通过和未通过的个数
{
if (t[head].grade == t[tail].grade) {
t[head].fail += t[tail].fail;
t[head].pass += t[tail].pass;
tail++;
}
else {
head++;
t[head] = t[tail];
tail++;
}
}
int nopass = 0;//统计所有分数(除最高分)未通过的个数
for (int i = 0; i < head; i++) {
nopass += t[i].fail;
}
d[head] = nopass+t[head].pass;//最高分预测正确个数为,小于最高分未通过个数+最高分通过个数
for (int i = head-1; i >= 0; i--) {//从高向低更新预测每个分数预测正确数
d[i] = d[i + 1] + t[i].pass - t[i].fail;
}
int out = 0;
int index=0;
for (int i = head; i >= 0; i--) {
if ( out < d[i]) {
index = i;
out = d[i];
}
}
cout << t[index].grade;
}
该程序读取输入的成绩和通过情况,按成绩从低到高排序,然后计算每个分数的通过和未通过人数。从最高分开始,预测正确个数为未通过人数加上当前分通过人数。接着遍历更低的分数,更新预测正确数。最后找出预测正确数最多的分数作为输出。
5648

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



