题的链接:P1068 分数线划定
题解: 先对数据排序,分数相同,学号小的在前,否则分数大的在前;用res标记是否到达限定的人数,pos标记总共取了多少人;当res到达限定res后就去判断后序的人和上一个人的分数是否相同,相同则保存,pos++,否则break;最后输出即可;
参考代码:
#include <string>
#include <iostream>
#include <algorithm>
using namespace std
;
int N
, M
, res
, pos
;
struct Node
{
int sno
;
int score
;
};
Node S
[5010];
bool cmp(Node a
, Node b
)
{
if(a
.score
== b
.score
) return a
.sno
< b
.sno
;
else return a
.score
> b
.score
;
}
int main()
{
cin
>> N
>> M
;
int m
= M
*1.5;
for(int i
= 0; i
< N
; i
++) cin
>> S
[i
].sno
>> S
[i
].score
;
sort(S
, S
+ N
, cmp
);
for(int i
= 0; i
< N
; i
++)
{
if(res
< m
) res
++; pos
++;
else if(res
== m
)
{
if(S
[i
].score
== S
[m
- 1].score
)
pos
++;
else break;
}
}
cout
<< S
[pos
- 1].score
<<" "<< pos
<< endl
;
for(int i
= 0; i
< pos
; i
++) cout
<< S
[i
].sno
<< " " << S
[i
].score
<< endl
;
return 0;
}
转载请注明原文地址: https://mac.8miu.com/read-516559.html