POJ 3664

来源:互联网 发布:python 自动完成 编辑:程序博客网 时间:2024/05/21 07:56
[NWPU][2014][TRN][1]水题堆
7:30:00
  • Overview
  • Problem
  • Status
  • Rank
  • Discuss
          
D - Election Time
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 3664

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 33 109 25 68 46 5

Sample Output

5

//@auther Yang Zongjun#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <cstring>#include <string>using namespace std;#define PI acos(-1.0)#define EPS 1e-8const int MAXN = 50005;const int INF = 2100000000;pair<int, int> p[MAXN];pair<int, int> a[MAXN];int n, k;int main(){    freopen("C:/Users/Administrator/Desktop/input.txt", "r", stdin);    scanf("%d%d", &n, &k);    for(int i = 1; i <= n; i++)    {        scanf("%d%d", &p[i].first, &p[i].second);        a[i].first = p[i].first;        a[i].second = p[i].second;    }    sort(p + 1, p + n + 1);    //由于sort排序之后,p的顺序,即牛的编号变了,所以我用    //了两个数组p和a    //for(int i = 1; i <= n; i++)        //printf("%d %d\n", p[i].first, p[i].second);    int maxa = p[n].second;    for(int i = n; i > n-k; i--)    {        maxa = max(maxa, p[i].second);    }    int j;    for(int i = 1; i <= n; i++)    {        if(a[i].second == maxa)        {            j = i;break;        }    }    printf("%d\n", j);    return 0;}

由于sort排序之后,p的顺序,即牛的编号变了,要开两个数组,而下面的代码用结构体,将牛的编号也设置成牛的属性,这样即使排序后每个牛的属性不变
//@auther Yang Zongjun#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <cstring>#include <string>using namespace std;#define PI acos(-1.0)#define EPS 1e-8const int MAXN = 50005;const int INF = 2100000000;int n, k;struct Cow{    int f,s,num;}cow[MAXN];bool cmp1(const Cow& a, const Cow& b){    return a.f > b.f;}bool cmp2(const Cow& a, const Cow& b){    return a.s > b.s;}int main(){//    freopen("C:/Users/Administrator/Desktop/input.txt", "r", stdin);    scanf("%d%d", &n, &k);    for(int i = 0; i < n; i++)    {        scanf("%d%d", &cow[i].f, &cow[i].s);        cow[i].num = i + 1;    }    sort(cow, cow + n, cmp1);    sort(cow, cow + k, cmp2);    printf("%d\n", cow[0].num);    return 0;}


0 0
原创粉丝点击