fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <cstdio>
  5. #include <cstring>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. int n,t[100],a[100],q[100*100];
  11. bool isOK, check;
  12.  
  13. void solve(){
  14. vector<int> v1,v2;
  15. v1.clear(); v2.clear();
  16. for(int i=1; i<=n; i++)
  17. if (a[i]>0) v1.push_back(i);
  18. else
  19. v2.push_back(i);
  20. v1.push_back(v1[0]);
  21. for(int i=0; i<v1.size()-1; i++)
  22. {
  23. int u,v;
  24. u=v1[i];
  25. v=v1[i+1];
  26. t[u]=v;
  27. a[v]--;
  28. }
  29. int j=-1;
  30. for(int i=1; i<=n; i++)
  31. while (a[i]>0){
  32. j++;
  33. if (j==v2.size()) {
  34. cout << -1 << endl;
  35. return;
  36. }
  37. t[v2[j]]=i;
  38. a[i]--;
  39. }
  40. for(int i=1; i<=n; i++) cout << t[i] << ' ';
  41. cout << endl;
  42.  
  43. }
  44.  
  45. int main(){
  46. //freopen("test.in","r",stdin);
  47. int total, ntest;
  48. cin >> ntest;
  49. while (ntest--){
  50. cin >> n;
  51. total = 0;
  52. isOK = true;
  53. memset(a, 0, sizeof(a));
  54. memset(t, 0, sizeof(t));
  55. for(int i=1; i<=n; i++) {
  56. cin >> a[i];
  57. total += a[i];
  58. if (a[i]==n) isOK = false;
  59. }
  60.  
  61. if (total!=n) isOK = false;
  62. if (!isOK)
  63. cout << -1 << endl;
  64. else{
  65. solve();
  66. }
  67. }
  68. return 0;
  69. }
Runtime error #stdin #stdout 0s 3316KB
stdin
Standard input is empty
stdout
Standard output is empty