admin管理员组

文章数量:1530842

Election Time
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6174 Accepted: 3340

Description

The cows are having their first election after overthrowing the tyrannical Farmer John, and Bessie is one ofN 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 ≤KN) 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 theAi list; likewise, no vote count appears twice in theBi list.

Input

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

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只牛入围,第二次竞选从入围的牛进行竞选第一名。

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

#include<iostream>
#define FIRST 1
#define SECOND 2
#define SIZE 50005
using namespace std;

void swap(long &a, long &b)
{
	long tmp;
	tmp = a;
	a = b;
	b = tmp;
}

long partition(long (&cows)[SIZE][3], long l, long r, int z)
{
	long x,i,j,k;
	x = cows[r][z];
	i = l - 1;
	for(j = l; j <= r - 1; j++)
	{
		if(cows[j][z] >= x)
		{
			i++;
			for(k = 0; k <= 2; k++)
			{
				swap(cows[i][k],cows[j][k]);
			}			
		}
	}
	for(k = 0; k <= 2; k++)
	{
		swap(cows[i + 1][k],cows[r][k]);
	}
	return i + 1;
}

void qsort(long (&cows)[SIZE][3], long l, long r, int z)
{
	long q;
	if(l < r)
	{
		q = partition(cows, l, r, z);
		qsort(cows, l, q - 1, z);
		qsort(cows, q + 1, r, z);
	}
}

int main()
{
	long cows[SIZE][3]; //number first second 
	long n,k,i;
	cin >> n >> k;
	for(i = 1; i <= n; i++)
	{
		cows[i][0]= i;
		cin >> cows[i][1] >> cows[i][2];
	}
	qsort(cows,1,n,FIRST);
	qsort(cows,1,k,SECOND);
	cout << cows[1][0] << endl;
	return 0;
}


本文标签: ElectionTIME