fork(1) download
  1. #include<iostream>
  2. #include<stack>
  3. #include<vector>
  4.  
  5. using namespace std;
  6. typedef long long LL;
  7.  
  8. int main()
  9. {
  10. int t=0;
  11. cin>> t;
  12. while(t--){
  13. LL n;
  14. stack<int>A,B;
  15.  
  16. cin>> n;
  17. vector<int>inpt,outp(n);
  18.  
  19. //for entering the data to the loop
  20. int ele;
  21. for(LL i= 0;i< n;i++ ){
  22. cin >> ele; A.push(ele); inpt.push_back(ele);
  23.  
  24. }
  25.  
  26. //for(int i=0;i< inpt.size();i++) cout<< inpt[i]<< endl;
  27.  
  28.  
  29.  
  30. //for next permutation.
  31.  
  32. long m,bin=0, t=1;LL num = n-1;
  33. bin = A.top(); A.pop();
  34. B.push(bin);
  35.  
  36. while(!A.empty() && t){
  37. m = A.top(); A.pop();
  38. if(m>= bin){
  39. bin = m;
  40. B.push(bin);
  41. }
  42. else{
  43. t=0;
  44. }
  45. num--;
  46. }
  47.  
  48.  
  49. while(!B.empty()){
  50. A.push(B.top());B.pop();
  51. }
  52.  
  53. if(num!=0){
  54. A.push(m);
  55. }
  56.  
  57. for(LL i=n-1;i>=0;i--){
  58. outp[i]= A.top();A.pop();
  59.  
  60. }
  61.  
  62.  
  63.  
  64. if(inpt == outp){cout<< -1;}
  65. else{for(LL i=0;i<outp.size();i++)
  66. cout<< outp[i];}
  67.  
  68.  
  69. cout<< endl;
  70. }
  71.  
  72. return 0;
  73. }
Success #stdin #stdout 0s 4372KB
stdin
1
2
0 0
stdout
-1