fork(1) download
  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4. int main(){
  5. int t,n,a[1000001];
  6. scanf("%d",&t);
  7. while(t){
  8. t--;
  9. int c=-1,temp,r,i,j;
  10. scanf("%d",&n);
  11. for(int j=0;j<n;j++)scanf("%d",&a[j]);
  12.  
  13. for(i=n-1;i>=0;i--)
  14. {
  15. for(j=i-1;j>=0;j--)
  16. {
  17. if(a[j]<a[i])
  18. {
  19. temp=a[i];
  20. a[i]=a[j];
  21. a[j]=temp;
  22. c=1;
  23. r=j;
  24. break;
  25. }
  26. }
  27. if(c==1)
  28. break;
  29. }
  30. if(c==1){
  31. sort(a+r,a+n);
  32. for(int j=0;j<n;j++)printf("%d",a[j]);
  33. printf("\n");
  34. }
  35. else{
  36. printf("%d\n",c);}
  37. }
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0s 6928KB
stdin
2

5

1 5 4 8 3

10

1 4 7 4 5 8 4 1 2 6
stdout
13458
1474584126