Election Time
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8780 | Accepted: 4591 |
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 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.
Input
* Line 1: Two space-separated integers: N and K
* Lines 2..N+1: Line i+1 contains two space-separated integers: Ai and Bi
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
Source
题意:n头牛要竞选总统,进行两轮投票,第一轮选出票数为前k名的进行第二轮投票,选出最高者为总统。
解:使用结构体,结构体中三个元素:count标记牛的位置,a为第一轮投票数量,b为第二轮投票数量。
对结构体数组进行针对a的从小到大排序,选出前k项,然后找出b最大的一头牛,输出count。
#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;
struct cows{
int count;
long long a;
long long b;
} cow[50005];
int cmp1(cows x,cows y){
if(x.a>y.a){
return 1;
}else return 0;
}
int cmp2(cows x,cows y){
if(x.b>y.b){
return 1;
}else return 0;
}
int main()
{
int n,k;
while(~scanf("%d%d",&n,&k)){
for(int i=0;i<n;i++){
cow[i].count=i+1;
scanf("%I64d %I64d",&cow[i].a,&cow[i].b);
}
sort(cow,cow+n,cmp1);
//printf("%I64d %I64d\n",cow[0].a,cow[1].a);
sort(cow,cow+k,cmp2);
printf("%d",cow[0].count);
}
}