Election Time
Time Limit:3000MS Memory Limit:65536K
Total Submit:49 Accepted:25
Description
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 A_i votes (1 <= A_i <= 1,000,000,000)in the first round and B_i votes (1 <= B_i <=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 A_i list; likewise, no vote count appears twice in the B_i list.
Input
* Line 1: Two space-separated integers: N and K
* Lines 2..N+1: Line i+1 contains two space-separated integers: A_i and B_i
Output
* Line 1: The index of the cow that is expected to win the election.
Sample Input
5 3 3 10 9 2 5 6 8 4 6 5
Sample Output
5
这个题目的意思是通过两次投票,选举总统,第一次投票选出票数最多的k人,
第二次,在选出的k人中再进行投票,票数最高的为总统。
本题只需两次调用qsort()函数即可。代码如下:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int x;
int y;
int num;
}a[51000];
int comp(const void *a,const void *b)
{
return (*(node *)a).x<(*(node *)b).x?1:-1;
}
int cmp(const void *a,const void *b)
{
return (*(node *)a).y<(*(node *)b).y?1:-1;
}
int main()
{
int m,k,i;
scanf("%d%d",&m,&k);
for(i=0;i<m;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
a[i].num=i+1;
}
qsort(a,m,sizeof(a[0]),comp);
qsort(a,k,sizeof(a[0]),cmp);
printf("%d/n",a[0].num);
return 0;
}