제출 #1329408

#제출 시각아이디문제언어결과실행 시간메모리
1329408vtnooGroup Photo (JOI21_ho_t3)C++20
100 / 100
3504 ms832 KiB
#include <bits/stdc++.h>
#define V vector
#define L(i,j,k) for(int i=(j);i<=(k);i++)
#define R(i,j,k) for(int i=(j);i>=(k);i--)
#define all(x) x.begin(),x.end()
#define sz(a) ((int)a.size())
#define pb push_back
using namespace std;
typedef long long ll;
const int INF=1e9;
int n;
struct STree{
	V<int>st;
	STree(){st.resize(n*4);}
	void upd(int p,ll x,int v=1,int s=0,int e=n){
		if(s==e){st[v]=x;return;}
		int m=(s+e)/2;
		if(p<=m)upd(p,x,v*2,s,m);
		else upd(p,x,v*2+1,m+1,e);
		st[v]=st[v*2]+st[v*2+1];
	}
	int que(int ts,int te,int v=1,int s=0,int e=n){
		if(e<ts||te<s)return 0;
		if(ts<=s&&e<=te)return st[v];
		int m=(s+e)/2;
		return que(ts,te,v*2,s,m)+que(ts,te,v*2+1,m+1,e);
	}
};
int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin>>n;
	V<ll>h(n),pos(n+1);
	L(i,0,n-1)cin>>h[i],pos[h[i]]=i;
	V<ll>dp(n+1,INF);
	dp[0]=0;
	L(i,0,n-1){
		STree pBlock,aBlock;
		L(j,1,i)pBlock.upd(pos[j],1);
		ll cost=0;
		L(j,i+1,n){
			cost+=pBlock.que(pos[j],n)+aBlock.que(0,pos[j]);
			aBlock.upd(pos[j],1);
			dp[j]=min(dp[j],dp[i]+cost);
		}
	}
	cout<<dp[n]<<endl;
}
#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...