对结构体的快速排序问题(用库函数)

题目链接:sdut 2695

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

两次快排就行,用c++中的库函数,很快就能做出来,关键就是如何对结构体排序的问题:
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 struct vode
 5 {
 6     int u,v,flag;
 7 }f[500000];
 8 int cmp(const void *a,const void *b)
 9 {
10     return ((struct vode *)b)->u-((struct vode *)a)->u;
11 }
12 int cp(const void *a,const void *b)
13 {
14     return ((struct vode *)b)->v-((struct vode *)a)->v;
15 }
16 int main()
17 {
18     int i;
19     for(i=0;i<=50000;i++)
20     {
21         f[i].u=0;
22         f[i].v=0;
23         f[i].flag=i;
24     }
25     int m,n;
26     scanf("%d%d",&m,&n);
27     for(int i=0;i<=m-1;i++)
28     {
29         int u,v;
30         scanf("%d%d",&u,&v);
31         f[i].u=u;
32         f[i].v=v;
33     }
34     qsort(f,m,sizeof(f[0]),cmp);
35     qsort(f,n,sizeof(f[0]),cp);
36     printf("%d",f[0].flag+1);
37 }
View Code

 

 

转载于:https://www.cnblogs.com/kuangdaoyizhimei/p/3249995.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值