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
*/
Ural1100 最终排名算法
这篇博客介绍了Ural1100比赛的最终排名问题,由于参赛队伍过多,旧的使用冒泡排序的方法效率低下。博主提出了一个快速生成与冒泡排序相同结果的算法,该算法按解决问题的数量降序排列,如果有相同的数量,则保持原始顺序。博客内容包括问题描述、解题思路以及代码实现。
1436

被折叠的 条评论
为什么被折叠?



