The5threic1-1

Calci_region_slow

Apr 22nd, 2024
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | Source Code | 0 0
  1. #include<bits/stdc++.h>
  2. #define endl '\n'
  3. using namespace std;
  4. #ifdef  ONLINE_JUDGE
  5.     #define eprintf(...) 42
  6. #else
  7.         #define eprintf(...) {fprintf(stderr, __VA_ARGS__);fflush(stderr);}
  8. #endif
  9. clock_t startTime;
  10. double getCurrentTime()
  11. {
  12.     return (double)(clock() - startTime) / CLOCKS_PER_SEC;
  13. }
  14. long long solve(vector<int> heights)
  15. {
  16.     int n = heights.size();
  17.     long long ans = 0;
  18.     for(int i=0; i<n; i++)
  19.     {
  20.         int l = i, r = i;
  21.         while(l>=0)
  22.         {
  23.             if(heights[l]>heights[i])
  24.             {
  25.                 break;
  26.             }
  27.             l--;
  28.         }
  29.         l++;
  30.         while(r<n)
  31.         {
  32.             if(heights[r]>heights[i])
  33.             {
  34.                 break;
  35.             }
  36.             r++;
  37.         }
  38.         r--;
  39.         ans += r - l + 1;
  40.     }
  41.     return ans;
  42. }
  43. int main()
  44. {
  45.     ios::sync_with_stdio(false);
  46.     cin.tie(0);
  47.     int t=1;
  48.     //cin>>t;
  49.     startTime = clock();
  50.     while(t--)
  51.     {
  52.         startTime = clock();
  53.         int n;
  54.         cin>>n;
  55.         vector<int> heights(n);
  56.         for(int i=0; i<n; i++)
  57.         {
  58.             cin>>heights[i];
  59.         }
  60.         cout<<solve(heights)<<endl;
  61.     }
  62.     eprintf("time = %.5lf\n", getCurrentTime());
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment