#include <bits/stdc++.h>
using namespace std;
#define f1 first
#define s2 second
#define INF 1e9+10
#define ll long long
#define pb push_back
#define fri(a) freopen(a,"r",stdin)
#define fro(a) freopen(a,"w",stdout)
const int N=2e5+5;
ll n,dizi[N];
pair<ll,int>t[4*N];
void build(int v,int tl,int tr){
if(tl==tr)t[v]={dizi[tl],tl};
else{
int tm=(tl+tr)/2;
build(v*2,tl,tm);
build(v*2+1,tm+1,tr);
t[v]=min(t[v*2],t[v*2+1]);
}
}
pair<ll,int> getmin(int v,int tl,int tr,int l,int r){
if(l>r)return {INF,-1};
if(l==tl && r==tr)return t[v];
int tm=(tl+tr)/2;
return min(getmin(v*2,tl,tm,l,min(r,tm)),getmin(v*2+1,tm+1,tr,max(l,tm+1),r));
}
int solve(int l,int r,int y){
//cout<<l<<" "<<r<<endl;
if(l>r)return 0;
//cout<<l<<" "<<r<<endl;
pair<ll,int> x=getmin(1,0,n,l,r);
return solve(l,x.s2-1,x.f1)+solve(x.s2+1,r,x.f1)+(y!=x.f1);
}
int main(){
// fri("in.txt");
// fro("out.txt");
cin>>n;
for(int i=0;i<n;i++){
cin>>dizi[i];
}
build(1,0,n);
cout<<solve(0,n,-1)-1<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
24 ms |
3052 KB |
Output is correct |
5 |
Correct |
35 ms |
5356 KB |
Output is correct |
6 |
Correct |
86 ms |
10348 KB |
Output is correct |
7 |
Correct |
71 ms |
9068 KB |
Output is correct |