# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1112877 | duyhoanho | Real Mountains (CCO23_day1problem2) | C++17 | 1 ms | 336 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
const int N = 5e3 + 10 ,M = 1e6 + 3;
int n , h[N];
int peak,ans,cnt[N] , sum[N];
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
#define task "task"
if(fopen(task".inp","r"))
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
if(fopen("task.inp","r"))
{
freopen("task.inp","r",stdin);
freopen("task.out","w",stdout);
}
cin >> n;
for(int i = 1; i <=n;i++) sum[i] = i + sum[i - 1];
for(int i = 1; i <= n; i++) cin >> h[i] , cnt[h[i]]++;
for(int i = 1; i < n;i++){
if(h[i + 1] > h[i]){
peak = i;
break;
}
}
if(peak == 0) peak = n;
int l = peak;
int r = peak;
while(true){
while(h[l] <= h[peak] && l > 0) l--;
while(h[r] <= h[peak] && r <= n) r++;
if(l == 0 || r == n + 1){
cout<<ans;
return 0;
}
int tmp = min(h[l] , h[r]);
ans = (ans + (tmp - h[peak]) * (h[l] + h[r]) * cnt[h[peak]]) % M;
ans = (ans + (sum[tmp - 1] - sum[h[peak] - 1]) * cnt[h[peak]]) % M;
if(h[l] <= h[r]) cnt[h[l]]+=cnt[h[peak]];
else cnt[h[r]]+=cnt[h[peak]];
h[peak] = tmp;
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |