zoukankan      html  css  js  c++  java
  • POJ3664

    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
    两次排序
    代码
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #define maxn 20020
    
    using namespace std;
    struct node{
        int a,b,num;
    }w[50010+10];
    bool cmp(node X,node Y)
    {
        return X.a>Y.a;
    }
    
    int main()
    {
        int n,k;
        while(~scanf("%d%d",&n,&k))
        {
            for(int i=0;i<n;i++)
            {
                scanf("%d%d",&w[i].a,&w[i].b);
                w[i].num=i+1;
            }
            sort(w,w+n,cmp);
            int Max = -1,ans;
            for(int i=0;i<k;i++)
            {
                if(Max<w[i].b)
                {
                    Max=w[i].b;
                    ans=w[i].num;
                }
            }
            cout <<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    makefile中的wildcard和patsubst
    makefile中=,:=,?=,+=区别
    hash函数查找和ASL计算
    ubuntu apt-get提示no dependencys怎么办
    增广贤文是不多的古典珍宝之一
    如何打印查看c++stack<..>中的内容(不使用pop,top)
    c/c++标准IO重定向
    c/c++使用#define,#ifdef,#endif将debug代码分离
    未完待续
    c++重载覆盖重定义多态其他别名区别
  • 原文地址:https://www.cnblogs.com/--lr/p/6716101.html
Copyright © 2011-2022 走看看