fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.  
  8. int T;
  9. cin >> T;
  10.  
  11. while(T--)
  12. {
  13. int N;
  14. cin >> N;
  15.  
  16. int arr[N];
  17.  
  18. for(int i=0; i<N; i++)
  19. cin >> arr[i];
  20.  
  21. int count = 0;
  22.  
  23. for(int i=1; i<N; i++)
  24. {
  25. int save = arr[i];
  26. int j = i-1;
  27.  
  28. while(j >= 0 && arr[j] > save)
  29. {
  30. arr[j+1] = arr[j--];
  31. count++;
  32. }
  33.  
  34. arr[j+1] = save;
  35. }
  36.  
  37. cout << count << endl;
  38. }
  39.  
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0s 16064KB
stdin
2  
5  
1 1 1 2 2  
5  
2 1 3 1 2
stdout
0
4