您的位置:首页 > 理论基础 > 数据结构算法

ural 1100. Final Standings(数据结构)

2013-12-10 17:29 961 查看

1100. Final Standings

Time limit: 1.0 second
Memory limit: 16 MB

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 Nlines 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 as produced by bubble sort (see below).

Sample

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

题意很简单。。。。

原以为用sort排个序即可。。。

wrong了后才发现,在value相等的情况下,不能对元素进行交换。。而sort(用的是快排,是不稳定的排序方式,因而会打乱顺序)

那要怎么办呢?

问了学长才知道有个叫做stable_value的东西~~

所谓stable_sort,是指对一个序列进行排序之后,如果两个元素的值相等,则原来乱序时在前面的元素现在(排好序之后)仍然排在前面。STL中提供stable_sort()函数来让我们进行稳定排序。为了更好的说明稳定排序的效果,我们定义了一个结构体元素,一个value成员和一个index成员,前者表示元素的值,后者表示乱序时的索引。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>

using namespace std;
struct node
{
long id;
int m;
bool operator<(const node&temp) const
{
return m>temp.m;
}
}kiss[150000+5];

int main()
{
//    freopen("input.txt","r",stdin);
int n;
while(cin>>n){
for(int i=0;i<n;i++){
scanf("%ld%d",&kiss[i].id,&kiss[i].m);
}
stable_sort(kiss,kiss+n);
for(int i=0;i<n;i++){
printf("%ld %d\n",kiss[i].id,kiss[i].m);
}
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: