Election Time

时间:2023-03-05 09:02:26

Election Time

Time Limit: 1000MS Memory limit: 65536K

题目描述

The cows are having their first election after overthrowing the tyrannical Farmer John, and Bessie is one of N cows (1 ≤ N ≤ 50,000) running for President. Before the election actually happens, however, Bessie wants to determine who has the best chance of winning.
The election consists of two rounds. In the first round, the K cows (1 ≤ K ≤ N) cows with the most votes advance to the second round. In the second round, the cow with the most votes becomes President.
Given that cow i expects to get Ai votes (1 ≤ Ai ≤ 1,000,000,000) in the first round and Bi votes (1 ≤ Bi ≤ 1,000,000,000) in the second round (if he or she makes it), determine which cow is expected to win the election. Happily for you, no vote count appears twice in the Ai list; likewise, no vote count appears twice in the Bi list.

输入

 Line 1: Two space-separated integers: N and K
Lines 2..N+1: Line i+1 contains two space-separated integers: Ai and Bi

输出

 The index of the cow that is expected to win the election.

示例输入

5 3
3 10
9 2
5 6
8 4
6 5

示例输出

5

题目意思,一些牛进行竞选,竞选两次,第一次有K只牛入围,第二次竞选从入围的牛进行竞选第一名。

思路,改进快速排序,进行两次即可,不多说了具体代码如下:

 #include <cstdio>
#include <algorithm>
using namespace std;
struct node
{
int a,b,num;
} s[];
int cmpa(node a, node b)
{
if (a.a == b.a) return a.b > b.b;
return a.a > b.a;
}
int cmpb(node a, node b)
{
if (a.b == b.b) return a.a > b.a;
return a.b > b.b;
}
int main()
{
int i,n,k;
while (scanf("%d%d", &n, &k) != EOF)
{
for (i = ; i < n; ++i)
{
scanf("%d%d", &s[i].a, &s[i].b);
s[i].num = i + ;
}
sort(s, s + n, cmpa);
sort(s, s + k, cmpb);
printf("%d\n", s[].num);
}
return ;
}