[Codeforces Round #101 (Div. 2) C. Queue] STL之vector

[Codeforces Round #101 (Div. 2) C. Queue] STL之vector

题目链接[Codeforces Round #101 (Div. 2) C. Queue]
题意描述:有N个人排队,现在告诉你每个人前面有 ai 个人身高比他高。( 1N3000,0aiN1 )将队伍打乱之后,现在要你还原这个队伍。并且输出一种可能的结果,即每个人的身高 hi(1hi109)
解题思路
引用官方题解:

Let’s sort the pairs (namei, ai) by ascending of ai. If there is an index i: 0 ≤ i < n that ai > i, then answer is “-1”. Otherwise the answer exists. We will iterate through the array of sorted pairs from left to right with supporting of vector of results res. Let on the current iteration ai = n - i, then we must transfer the current man in the position ai. It can be done in C++ with one line: res.insert(res.begin() + a[i], man);

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

#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
#define fst             first
#define snd             second

typedef __int64 LL;
//typedef long long LL;
typedef pair<int, int> PII;

const int MAXN = 3000 + 5;

int N, M, K;
char buf[MAXN][15];
PII ele[MAXN];
vector<PII> res;

int main() {
#ifndef ONLINE_JUDGE
    FIN;
#endif // ONLINE_JUDGE
    while (~scanf ("%d", &N) ) {
        for (int i = 1; i <= N; i++) {
            scanf ("%s %d", buf[i], &ele[i].fst);
            ele[i].snd = i;
        }
        sort (ele + 1, ele + N + 1);
        bool suc = true;
        for (int i = 1; i <= N; i++) {
            if (ele[i].fst >= i) suc = false;
        }
        if (!suc) {
            printf ("-1\n");
            continue;
        }
        res.clear();
        for (int i = 1, k = N; i <= N; i++, k--) {
            res.insert (res.begin() + ele[i].fst, make_pair (ele[i].snd, k) );
        }
        for (int i = 0; i < N; i++) {
            PII& e = res[i];
            printf ("%s %d\n",  buf[e.fst], e.snd);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值