Ural1100 Final Standings
Find Standings(Data Structure)
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 1 < N ≤ 150000 1<N≤150000 — number of teams. Each of the next N lines contains two integers 1 ≤ I D ≤ 107 1 ≤ ID ≤ 107 1≤ID≤107 and 0 ≤ M ≤ 100 0 ≤ M ≤ 100 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).
大致题意:
按第二元素排序,若两者相同,不改变两者原来的相对前后关系。
这算个比较脑残的题了吧。。。直接sort + 记录一下初始位置即可,没啥好说的
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1000005;
int n;
inline int read()
{
int s=0,f=1;
char c=getchar();
while (c<'0'||c>'9')
{
if (c=='-')
{
f=-1;
}
c=getchar();
}
while (c>='0'&&c<='9')
{
s=s*10+c-48;
c=getchar();
}
return s*f;
}
struct node
{
int x,y,id;
}a[maxn];
int cmp(node a, node b)
{
if (a.y==b.y)
{
return a.id<b.id;
}
return a.y>b.y;
}
int main()
{
n=read();
for (int i=1;i<=n;i++)
{
a[i].x=read(),a[i].y=read();
a[i].id=i;
}
sort(a+1,a+n+1,cmp);
for (int i=1;i<=n;i++)
{
cout<<a[i].x<<" "<<a[i].y<<endl;
}
return 0;
}
/*
8
1 2
16 3
11 2
20 3
3 5
26 4
7 1
22 4
*/