zoukankan      html  css  js  c++  java
  • Election Time

    Election Time

    Time Limit: 1000MS Memory limit: 65536K

    题目描述

    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.

    输入

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

    输出

     The index of the cow that is expected to win the election.

    示例输入

    5 3
    3 10
    9 2
    5 6
    8 4
    6 5

    示例输出

    5

    题目意思,一些牛进行竞选,竞选两次,第一次有K只牛入围,第二次竞选从入围的牛进行竞选第一名。

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

     1 #include <cstdio>
     2 #include <algorithm>
     3 using namespace std;
     4 struct node
     5 {
     6     int a,b,num;
     7 } s[50010];
     8 int cmpa(node a, node b)
     9 {
    10     if (a.a == b.a) return a.b > b.b;
    11     return a.a > b.a;
    12 }
    13 int cmpb(node a, node b)
    14 {
    15     if (a.b == b.b) return a.a > b.a;
    16     return a.b > b.b;
    17 }
    18 int main()
    19 {
    20     int i,n,k;
    21     while (scanf("%d%d", &n, &k) != EOF)
    22     {
    23         for (i = 0; i < n; ++i)
    24         {
    25             scanf("%d%d", &s[i].a, &s[i].b);
    26             s[i].num = i + 1;
    27         }
    28         sort(s, s + n, cmpa);
    29         sort(s, s + k, cmpb);
    30         printf("%d
    ", s[0].num);
    31     }
    32     return 0;
    33 }
    View Code
  • 相关阅读:
    C++ 三数之和
    C++ 保存读取二进制
    流媒体 Ubuntu部署srs、windows部署nginx
    sql函数(三)MERGE INTO函数
    sql函数(二)LISTAGG()函数(列转行函数)
    sql函数(一)ROW_NUMBER() OVER()--表内分组并排序
    java注释(类、构造函数、方法、代码块、单行、属性注释)
    springBoot注解整理
    @DateTimeFormat与@JsonFormat作用
    DataJPA简述与注解
  • 原文地址:https://www.cnblogs.com/kongkaikai/p/3250364.html
Copyright © 2011-2022 走看看