题意:n 个人在排队,然后给出每个人的前面比他身高高的人的数量hi,让你给出一种排列,并给出一种解。
析:首先,hi 小的要在前面,所以先进行排序,然后第一个人的 h1 必须为0,我们可以令身高为 1,然后对于第 i 个人,前面1 ~ i-1 个人中有 hi 个人
比他高,那么就有 i-1-hi 个人不比他高,所以他的身高的最佳情况就是 i - hi。不过这样会覆盖前面那个身高和他相等的人,所以我们把 1 ~ i-1 中所有的
身高比他高的人都加上1,这样就消除了影响。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const LL LNF = 1e16; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 3000 + 10; const int mod = 1000000007; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } struct Node{ string name; int num; Node() { } Node(string s, int n) : name(s), num(n) { } bool operator < (const Node &p) const{ return num < p.num; } }; Node a[maxn]; const int det = 100000; int main(){ cin >> n; for(int i = 0; i < n; ++i) cin >> a[i].name >> a[i].num; sort(a, a + n); if(1 == n && a[0].num){ cout << "-1" << endl; return 0; } a[0].num = 1; for(int i = 1; i < n; ++i){ if(a[i].num > i){ cout << "-1" << endl; return 0; } a[i].num = i - a[i].num + 1; for(int j = 0; j < i; ++j) if(a[j].num >= a[i].num) ++a[j].num; } for(int i = 0; i < n; ++i) cout << a[i].name << " " << a[i].num + det << endl; return 0; }