fork download
  1. #include <bits/stdc++.h>
  2.  
  3. #define ll long long
  4. #define el cout << '\n'
  5.  
  6. using namespace std;
  7.  
  8. const int maxn = 1e7;
  9. const int INF = 1e9;
  10.  
  11. int n, a[maxn + 10], l[maxn + 10], r[maxn + 10], cnt[maxn + 10], mx = 0, j = maxn, ans;
  12. vector<int> stk, pos[maxn + 10];
  13.  
  14. void update()
  15. {
  16. while (cnt[j] == 0)
  17. j--;
  18. }
  19.  
  20. int main()
  21. {
  22. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  23. if (fopen("QUANTIEUPHU.INP", "r"))
  24. {
  25. freopen("QUANTIEUPHU.INP", "r", stdin);
  26. freopen("QUANTIEUPHU.OUT", "w", stdout);
  27. }
  28.  
  29. cin >> n;
  30. for (int i = 1; i <= n; i++)
  31. {
  32. cin >> a[i];
  33. pos[a[i]].push_back(i);
  34. mx = max(mx, a[i]);
  35. }
  36. a[0] = a[n + 1] = INF;
  37. stk.push_back(0);
  38.  
  39. for (int i = 1; i <= n; i++)
  40. {
  41. while (a[i] > a[stk.back()])
  42. stk.pop_back();
  43. l[i] = stk.back();
  44. stk.push_back(i);
  45. }
  46. stk.clear();
  47. stk.push_back(n + 1);
  48. for (int i = n; i >= 1; i--)
  49. {
  50. while (a[i] >= a[stk.back()])
  51. stk.pop_back();
  52. r[i] = stk.back();
  53. stk.push_back(i);
  54. }
  55. for (int x : pos[mx])
  56. cnt[x - l[x]]++;
  57. for (int i = mx - 1; i >= 0; i--)
  58. {
  59. for (int x : pos[i])
  60. {
  61. cnt[r[x] - l[x]]--;
  62. cnt[x - l[x]]++;
  63. cnt[r[x] - x]++;
  64. }
  65. update();
  66. if (j <= i)
  67. ans = j;
  68. }
  69. cout << ans;
  70. }
  71.  
Success #stdin #stdout 0.05s 240764KB
stdin
Standard input is empty
stdout
Standard output is empty