Codeforces 141C【构造】

题意

有n个在排队,现给出n个人前面有多少个人比他的个数,让你构造这个排列。

思路

一种想法:我们现在知道每个人前面有多少个人,那么我们构造这个人的序列。先把给出的数量进行排序,我们可以知道每个位置之前人比他高数量最多的情况是 0,1,2,3,4,5,6,7... ,如果不满足直接输出”-1”;然后我们
直接模拟下来,把每个人都放到 (pos=+1) 位置,然后每个人的高度就是 INF(pos=)

Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<map>
#include<queue>
#include<cmath>
#include<algorithm>
#include<deque>
using namespace std;
typedef long long LL;
//#pragma comment(linker, "/STACK:102400000,102400000")

const double PI = acos(-1.0);
const double eps = 1e-6;
const int N = 3e3+10;
const int INF=1e9;

struct asd{
    char name[15];
    int num;
}p[N];
int mp[N];
int n,cnt[N];
bool cmp(asd x,asd y){
    return x.num<y.num;
}

int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%s%d",p[i].name,&p[i].num);
    sort(p+1,p+n+1,cmp);
    memset(cnt,-1,sizeof(cnt));
    for(int i=1;i<=n;i++){
        if(p[i].num>i-1){
            puts("-1");
            return 0;
        }
        int pos=p[i].num+1;
        int temp,t;
        temp=cnt[pos];
        cnt[pos]=i;
        mp[i]=pos;
        t=pos+1;
        while(temp!=-1)
        {
            int tt=cnt[t];
            cnt[t]=temp;
            mp[temp]=t;
            temp=tt;
            t++;
        }
    }
    for(int i=1;i<=n;i++)
        p[cnt[i]].num = INF-i+1;
    for(int i=1;i<=n;i++)
        printf("%s %d\n",p[i].name,p[i].num);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值