zoukankan      html  css  js  c++  java
  • poj 3664 Election Time

    题目描述

    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
    
    题意:输入N行数据 求出前k项中第二轮得票最多的是哪一项
     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #define N 50005
     4 
     5 typedef struct cow
     6 {
     7     int ai,bi,data;//ai第一列数据,bi第二列数据,data存储位置信息
     8 } Cow;
     9 Cow a[N];
    10 
    11 /*int型数据qsort排序方法头文件位于#include<stdlib.h>
    12 格式:
    13 int cmp ( const void *a , const void *b )
    14 {
    15 return *(int *)a - *(int *)b;由小到大排序
    16 }
    17 qsort(num,100,sizeof(num[0]),cmp);*/
    18 
    19 int cmp ( const void *a , const void *b )
    20 {
    21     return *(int *)b - *(int *)a;
    22 }
    23 
    24 int main()
    25 {
    26     int n,k,i,max,j;
    27     scanf("%d%d",&n,&k);
    28     for(i=0; i<n; i++)
    29     {
    30         scanf("%d%d",&a[i].ai,&a[i].bi);
    31         a[i].data=i+1;
    32     }
    33     qsort(a,n,sizeof(Cow),cmp);//int型数据qsort排序方法头文件位于#include<stdlib.h>
    34     for(i=0,max=0; i<k; i++)
    35     {
    36         if(max<a[i].bi)
    37         {
    38             max = a[i].bi;
    39             j = i;
    40         }
    41     }
    42     printf("%d
    ",a[j].data);
    43     return 0;
    44 
    45 }
  • 相关阅读:
    如何快速上手LINQ to XML
    协变还是逆变,这还是个问题吗
    WPF的依赖属性是怎么节约内存的?
    CLR线程总结
    关于.NET异常 你应该知道的更多点
    [TED] Kinect控制的四翼直升机
    vSphere系列Windows环境安装部署嵌入式PSC的vCenter Server
    vSphere系列Linux环境部署安装非嵌入式PSC的vCenter Server Appliance
    Go 初识
    vSphere系列vCenter Server入门_01.认识与安装
  • 原文地址:https://www.cnblogs.com/wlc297984368/p/3250116.html
Copyright © 2011-2022 走看看