[Codeforces Round #101 (Div. 2) C. Queue] STL之vector
题目链接:[Codeforces Round #101 (Div. 2) C. Queue]
题意描述:有N个人排队,现在告诉你每个人前面有
ai
个人身高比他高。(
1≤N≤3000,0≤ai≤N−1
)将队伍打乱之后,现在要你还原这个队伍。并且输出一种可能的结果,即每个人的身高
hi(1≤hi≤109)
。
解题思路:
引用官方题解:
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;
}