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\n",a[j].data);
43     return 0;
44 
45 }

 

转载于:https://www.cnblogs.com/wlc297984368/p/3250116.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值