URAL 1100

Problem Description
Old contest software uses bubble sort for generating final standings. But now, there are too many teams and that software works too slow. You are asked to write a program, which generates exactly the same final standings as old software, but fast.
 

 

Input
The first line of input contains only integer 1 < N ≤ 150000 — number of teams. Each of the next N lines contains two integers 1 ≤ ID ≤ 10 7 and 0 ≤ M ≤ 100. ID — unique number of team, M — number of solved problems.
 

 

Output
Output should contain N lines with two integers ID and M on each. Lines should be sorted by M in descending order using bubble sort (or analog).
 

 

Sample Input
inputoutput
8
1 2
16 3
11 2
20 3
3 5
26 4
7 1
22 4
3 5
26 4
22 4
16 3
20 3
1 2
11 2
7 1

Hint

Bubble sort works following way:
while (exists A[i] and A[i+1] such as A[i] < A[i+1]) do
   Swap(A[i], A[i+1]);

对结构体进行稳定排序,stable_sort()内部实现是归并排序,sort()是不稳定的排序。

快速排序、希尔排序、堆排序、直接选择排序不是稳定的排序算法,

而基数排序、冒泡排序、直接插入排序、折半插入排序、归并排序是稳定的排序算法。

代码如下:

 1 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4 #include<iterator>
 5 
 6 using namespace std;
 7 
 8 typedef struct
 9 {
10     int id;
11     int m;
12 }node;
13 
14 bool cmp(const node &a, const node &b)
15 {
16     return b.m<a.m;
17 }
18 
19 int main()
20 {
21     vector <node> team;
22     int i, n;
23     int id, m;
24     node t;
25     cin >> n;
26     for (i=0; i<n; i++)
27     {
28         cin >> id >> m;
29         t.id=id;
30         t.m=m;
31         team.push_back(t);
32     }
33     stable_sort(team.begin(), team.end(), cmp);
34     vector<node>::iterator p;
35     for (p=team.begin(); p!=team.end(); p++)
36         cout << p->id << " " << p->m << endl;
37     return 0;
38 }

转载于:https://www.cnblogs.com/zrq495/archive/2012/07/27/2611795.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值