Ankit_132

D

Jun 20th, 2024
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. #define endl "\n"
  7. #define bug(x) cerr << #x << " = " << x << endl
  8. #define sz(x) (int)x.size()
  9. #define mp make_pair
  10. #define LL long long
  11.  
  12. int n;
  13.  
  14. void solve(){
  15.     cin >> n;
  16.     vector<int> a(n);
  17.     for(int i = 0; i < n; i ++) cin >> a[i];
  18.    
  19.     vector<int> vec; vec.clear();
  20.     int L = 0, R = n;
  21.     for(int i = 0; i < n - 1; i ++){
  22.         if(a[i] > a[i + 1]){
  23.             L = max(L, a[i] - a[i + 1]);
  24.             vec.push_back(i);
  25.         }
  26.     }
  27.    
  28.     if(vec.size() == 0) {
  29.         cout << (1ll + n) * n / 2 << endl;
  30.         return;
  31.     }
  32.    
  33.     for(int i = 0; i < vec.size() - 1; i ++) if(vec[i] + 1 == vec[i + 1]){
  34.         cout << 0 << endl;
  35.         return;
  36.     }
  37.    
  38.     for(int i = 0; i < vec.size() - 1; i ++){
  39.         int st = vec[i] + 1, en = vec[i + 1];
  40.         assert(st < en);
  41.         int tmx = 0;
  42.         while(st != en){
  43.             tmx = max(a[st + 1] - a[st], tmx);
  44.             st ++;
  45.         }
  46.         R = min(tmx, R);
  47.     }
  48.    
  49.     if(R < L) {
  50.         cout << 0 << endl;
  51.     }
  52.     else {
  53.         cout << ((long long)L + R) * (R - L + 1) / 2 << endl;
  54.     }
  55. }
  56.  
  57. int main(){
  58.     ios::sync_with_stdio(0);
  59.     cin.tie(0), cout.tie(0);
  60.  
  61.     int Tc = 1;
  62.     cin >> Tc;
  63.     while(Tc --){
  64.         solve();
  65.     }
  66.  
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment