zrq495
www.zrq495.com
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 ≤ 107 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 }
posted on 2012-07-27 14:49  zrq495  阅读(185)  评论(0编辑  收藏  举报