fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int min_operations_to_non_decreasing(vector<int>& heights) {
  6. int operations = 0;
  7. for (int i = 1; i < heights.size(); ++i) {
  8. if (heights[i] < heights[i - 1]) {
  9. // Increase heights[i] to heights[i - 1] to maintain non-decreasing order
  10. operations++;
  11. heights[i] = heights[i - 1];
  12. }
  13. }
  14. return operations;
  15. }
  16.  
  17. int main() {
  18. int t;
  19. cin >> t; // Number of test cases
  20. while (t--) {
  21. int n;
  22. cin >> n; // Number of pillars
  23. vector<int> heights(n);
  24.  
  25. for (int i = 0; i < n; ++i) {
  26. cin >> heights[i];
  27. }
  28.  
  29. int result = min_operations_to_non_decreasing(heights);
  30. cout << result << endl;
  31. }
  32.  
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0s 5280KB
stdin
3
5
5 4 3 2 1
3
2 2 1
1
1
stdout
4
1
0