N个数,第i个数代表i之前的还未参加比赛的人的人数。每来够三个人可以参加比赛,她们可以随时加入比赛(也可以暂时不加入),求一个可能的到达顺序满足题意
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <vector> 5 #define INF 0x3f3f3f3f 6 #define MOD 1000000007 7 using namespace std; 8 typedef long long LL; 9 10 const int maxn = 2e5 + 10; 11 int ans[maxn]; 12 int cnt[maxn]; 13 int num[maxn]; 14 vector<int> G[maxn]; 15 16 int nxt(int x) { 17 int up = x - 3, down = x % 3; 18 while (up > down) { 19 if (cnt[up]) return up; 20 up -= 3; 21 } 22 return down; 23 } 24 25 int main() { 26 int N; 27 scanf("%d", &N); 28 for (int i = 1; i <= N; i++) { 29 scanf("%d", &num[i]); 30 G[num[i]].push_back(i); 31 cnt[num[i]]++; 32 } 33 bool flag = 1; 34 int k = 0; 35 while (1) { 36 if (ans[0] == N) break; 37 if (cnt[k]) { 38 flag = 1; 39 int tmp = G[k][--cnt[k]]; 40 ans[++ans[0]] = tmp; 41 k++; 42 } else { 43 k = nxt(k); 44 if (flag == 0) break; 45 flag = 0; 46 } 47 } 48 if (ans[0] == N) { 49 puts("Possible"); 50 for (int i = 1; i <= ans[0]; i++) { 51 printf("%d%c", ans[i], i == ans[0] ? ‘\n‘ : ‘ ‘); 52 } 53 } else { 54 puts("Impossible"); 55 } 56 return 0; 57 }
时间: 2024-10-12 13:14:59