Ural1100 Final Standings

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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<N150000 — number of teams. Each of the next N lines contains two integers 1 ≤ I D ≤ 107 1 ≤ ID ≤ 107 1ID107 and 0 ≤ M ≤ 100 0 ≤ M ≤ 100 0M100. 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
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值