fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <deque>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9. int T;
  10. cin >> T;
  11.  
  12. while (T--) {
  13. int n;
  14. cin >> n;
  15.  
  16. vector<int> heights(n);
  17. for (int i = 0; i < n; ++i) {
  18. cin >> heights[i];
  19. }
  20.  
  21. int maxLength = 0;
  22.  
  23. // 使用滑动窗口方法
  24. int left = 0; // 左边界
  25. for (int right = 0; right < n; ++right) {
  26. while (heights[right] - heights[left] > (right - left)) {
  27. left++; // 调整左边界
  28. }
  29. maxLength = max(maxLength, right - left + 1); // 更新最大长度
  30. }
  31.  
  32. cout << maxLength << endl;
  33. }
  34.  
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0s 5324KB
stdin
3
5
3 6 2 3 1
6
1 1 4 5 1 4
10
7 8 3 5 6 1 2 4 9 10
stdout
4
4
10