Fighting!

欲戴皇冠,必承其重!

[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;
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ACMore_Xiong/article/details/52373109
文章标签: codeforces stl
个人分类: ACM____STL
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭