您的位置:首页 > 其它

poj 3664

2014-10-02 15:42 253 查看
Election Time

Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 7390
Accepted: 4000
Description
The cows are having their first electionafter overthrowing the tyrannical Farmer John, and Bessie is one of N cows(1 ≤ N ≤ 50,000) running for President. Before the electionactually happens, however, Bessie wants to determine
who has the best chance ofwinning.
The election consists of two rounds. Inthe first round, the K cows (1 ≤ K ≤ N)cows with the most votes advance to the second round. In the second round, thecow with the most votes becomes President.
Given that cow i expectsto 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 cowis expected to win the election. Happily for you, no vote count appears twicein the Ai list; likewise, no vote count appearstwice 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 isexpected to win the election.
Sample Input
5 3
3 10
9 2
5 6
8 4
6 5
Sample Output
5

解题思路:这道题目的题意很好理解,就是将头牛先根据Ai值从大到小排序,取出前K头Ai值大的牛,再根据这K头牛Bi的值,将它们从大到小排序,输出Bi值最大的那头牛的原始序号值。有关排序可以直接使用C++中的sort函数,难点在于,两次排序以及原始序号值的记录,这就需要使用到结构体。

源代码:

#inlcude<iostream>

#include<stdio.h>

#include<algorithm>

using namespace std;

struct vote

{

intA;

intB;

intnum;

}cow[50001];

int cmp1(vote cow1,vote cow2)

{

returncow1.A>cow2.A;

}

int cmp2(vote cow1,vote cow2)

{

returncow1.B>cow2.B;

}

int main()

{

intn,k,i;

vote cow[50001];

scanf("%d%d",&n,&k);

for(i=0;i<n;i++)

{

scanf("%d%d",&cow[i].A,&cow[i].B);

cow[i].num=i+1;

}

sort(cow,cow+n,cmp1);

sort(cow,cow+k,cmp2);

printf("%d\n",cow[0].num);

return0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: