ORZ翁教把URAL的数据结构刷完了,于是也尝试着开始刷URAL的数据结构。。。。FIGHTTING!
很囧的在URAL的第一跑就WRONG了,还是跪给了数据结构的第一题。
诶,好讨厌= =#。。。
以下是题目。。。。。。
1100. Final Standings
Time limit: 1.0 second
Memory limit: 16 MB
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
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 | output |
---|---|
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]);
Problem Author: Pavel Atnashev
Problem Source: Tetrahedron Team Contest May 2001
Problem Source: Tetrahedron Team Contest May 2001
Tags:
)
题意很简单。。。。
原以为用sort排个序即可。。。
wrong了后才发现,在value相等的情况下,不能对元素进行交换。。而sort(用的是快排,是不稳定的排序方式,因而会打乱顺序)
那要怎么办呢?
问了学长才知道有个叫做stable_value的东西~~
所谓stable_sort,是指对一个序列进行排序之后,如果两个元素的值相等,则原来乱序时在前面的元素现在(排好序之后)仍然排在前面。STL中提供stable_sort()函数来让我们进行稳定排序。为了更好的说明稳定排序的效果,我们定义了一个结构体元素,一个value成员和一个index成员,前者表示元素的值,后者表示乱序时的索引。
基础知识还是太弱啊!要多刷题!
以下是AC代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxnum=150010;
struct student
{
int id;
int val;
bool operator<(const student&temp) const
{
return val>temp.val;
}
}a[maxnum];
int main()
{
//freopen("input.txt","r",stdin);
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i)
scanf("%d%d",&a[i].id,&a[i].val);
stable_sort(a+1,a+1+n);
for(int i=1;i<=n;++i)
printf("%d %d\n",a[i].id,a[i].val);
return 0;
}