Queue CodeForces - 141C(排序+思维)

In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height h i, and the heights of the other people in the queue. Each of them keeps in mind number a i — how many people who are taller than him/her and stand in queue in front of him.

After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.

When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number a i.

Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers h i — the heights of people in the queue, so that the numbers a i are correct.

Input
The first input line contains integer n — the number of people in the queue (1 ≤ n ≤ 3000). Then n lines contain descriptions of the people as " name i a i" (one description on one line), where name i is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person’s name), a i is an integer (0 ≤ a i ≤ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.

Output
If there’s no acceptable order of the people in the queue, print the single line containing “-1” without the quotes. Otherwise, print in n lines the people as " name i h i", where h i is the integer from 1 to 109 (inclusive), the possible height of a man whose name is name i. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers h i are not necessarily unique.

Examples
Input
4
a 0
b 2
c 0
d 0
Output
a 150
c 170
d 180
b 160
Input
4
vasya 0
petya 1
manya 3
dunay 3
Output
-1
思路:显而易见,ai越大,排位就越靠后。而且这n个人我们可以用1-n这n个数字来表示的。那么我们按照ai由小到大排序然后逆向遍历,这样我们就可以判断出它所处的位置应该所具有的高度。如果这个高度找不到的话,那就是-1.
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=3e3+100;
struct node{
	string s;
	int id,h;
	bool operator<(const node &a)const{
		return h<a.h;
	}
}p[maxx];
int ans[maxx];
string S[maxx];
int n;

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) cin>>p[i].s>>p[i].h,p[i].id=i;
	sort(p+1,p+1+n);
	int flag=1,m=n;
	set<int> s;
	for(int i=1;i<=n;i++) s.insert(i);
	set<int> :: iterator it;
	while(n)
	{
		if(n-p[n].h-1<0) 
		{
			flag=0;
			break;
		}
		int x=n-p[n].h-1;
		it=s.begin();
		while(x--) it++;
		ans[n]=*it;
		S[n]=p[n].s;
		s.erase(*it);
		n--;
	}
	if(flag==0) cout<<-1<<endl;
	else for(int i=1;i<=m;i++) cout<<S[i]<<" "<<ans[i]<<endl;
	return 0;
}

努力加油a啊,(o)/~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值