Submission #968324

#TimeUsernameProblemLanguageResultExecution timeMemory
968324phoenix0423Real Mountains (CCO23_day1problem2)C++17
3 / 25
8 ms6744 KiB
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pll; #define fastio ios::sync_with_stdio(false), cin.tie(0) #pragma GCC optimize("Ofast") #define pb push_back #define eb emplace_back #define f first #define s second #define int long long #define lowbit(x) x&-x const int maxn = 1e6 + 1; const int N = 1e6 + 3; const int INF = 1e18; int h[maxn], l[maxn], r[maxn], cl[maxn], cr[maxn]; signed main(void){ fastio; int n; cin>>n; int mn = 0; for(int i = 0; i < n; i++){ cin>>h[i]; if(h[i] < h[mn]) mn = i; } for(int i = 0; i < n; i++) r[i] = -1; for(int i = 0; i < n; i++){ l[i] = r[i] = -1; for(int j = i - 1; j >= 0; j--){ if(h[j] > h[i]){ l[i] = j; break; } } for(int j = i + 1; j < n; j++){ if(h[j] >= h[i]){ r[i] = j; break; } } } int ans = 0; for(int i = 1; i < n; i++){ if(r[i] == -1 || l[i] == -1) continue; if(h[r[i]] == h[i]) continue; int cnt = r[i] - l[i] - 1; int dif = min(h[r[i]], h[l[i]]) - h[i]; // cout<<i<<" "<<cnt<<" "<<dif<<"\n"; // cout<<h[l[i]]<<" "<<h[r[i]]<<"\n"; ans += dif * (h[l[i]] + h[i] + h[r[i]]) + dif * (dif - 1) / 2; if(cnt >= 2) ans += dif * (min(h[l[i]], h[r[i]]) + h[i] * 2) + dif * (dif + 1) - dif; if(cnt > 2) ans += (cnt - 2) * (dif * h[i] * 3 + dif * (dif + 1) + dif * (dif - 1) / 2); // cout<<"ans : "<<ans<<"\n"; } cout<<ans % N<<"\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...