답안 #89846

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
89846 2018-12-18T15:07:34 Z giorgikob Building Bridges (CEOI17_building) C++14
100 / 100
94 ms 17628 KB
#include<bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define mod 1000000007
#define ll long long
using namespace std;
ll n, m, k, x, y, a, b, t, q, ans;

ll W[500001],H[500001],dp[500001],sum[500001],DP[500001],B[500001];
struct tree{
	tree* L;
	tree* R;
	ll k,b;
	tree(){
		k=b=0LL;
		L=R=NULL;
	}
};
tree* root=new tree();
double cross(double k,double b,double K,double B){
	//if(K-k==0)cout<<1<<endl;
	return (b-B)/(K-k);
}
void update(tree* node,ll l,ll r,ll K,ll B){
	ll K1=node->k;
	ll B1=node->b;
	if(K1==0 && B1==0){
		node->k=K;
		node->b=B;
		return ;
	}
	if(K1==K){
		if(B<B1){
			node->k=K;
			node->b=B;
		}
		return ;
	}
	double x=cross(K1,B1,K,B);
	ll y=x-10;
	bool ok=( y*K+B < y*K1+B1 );
	
	
	if(ok){
		if(x<l)return;
		if(x>r){
			node->k=K;
			node->b=B;
			return ;	
		}
		if(l==r)return ;
		ll mid=(l+r)/2;
		if(x<=mid) {
			if(node->L == NULL)
				node->L = new tree();
			update(node->L,l,mid,K,B);
		} else 
		if(x>mid) {
			if(node->R == NULL)
				node->R = new tree();
			node->k=K;
			node->b=B;
			update(node->R,mid+1,r,K1,B1);
		}
	} else {
		if(x>r)return;
		if(x<l){
			node->k=K;
			node->b=B;
			return ;	
		}
		if(l==r)return ;
		ll mid=(l+r)/2;
		if(x>mid) {
			if(node->R == NULL)
				node->R = new tree();
			update(node->R,mid+1,r,K,B);
		} else 
		if(x<=mid) {
			if(node->L == NULL)
				node->L = new tree();
			node->k=K;
			node->b=B;
			update(node->L,l,mid,K1,B1);
		}	
	}
}
ll get(tree* node,ll l,ll r,ll x){
	if(l==r)
		return node->k*x+node->b;
	ll mid=(l+r)/2;
	if(x<=mid){
		if(node->L==NULL)
			return node->k*x+node->b;
		else{
			ll y=get(node->L,l,mid,x);
			return min(y,node->k*x+node->b);		
		}
	} else {
		if(node->R==NULL)
			return node->k*x+node->b;
		else{
			ll y=get(node->R,mid+1,r,x);
			return min(y,node->k*x+node->b);
		}
	}	
}
main()
{
	ios::sync_with_stdio(0);
	//freopen("a.in","r",stdin);
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>H[i];
	
	for(int i=1;i<=n;i++)
		cin>>W[i],sum[i]=sum[i-1]+W[i];
	
	update(root,0LL,1e10,-2*H[1],H[1]*H[1]-sum[1]);
	for(int i=2;i<=n;i++){
		dp[i]=H[i]*H[i]+sum[i-1]+get(root,0LL,1e10,H[i]);
		//cout<<get(root,0,1e6,H[i])<<" ";
		update(root,0LL,1e10,-2*H[i],H[i]*H[i]+dp[i]-sum[i]);
		//cout<<dp[i]<<endl;
	}
	
	/*for(int i=2;i<=n;i++){
		DP[i]=1e18;
		for(int j=1;j<i;j++){
			DP[i]=min(DP[i],H[i]*H[i]+sum[i-1]-2*H[i]*H[j]+H[j]*H[j]+DP[j]-sum[j]);
		}
		for(int j=1;j<i;j++){
			if(DP[i]==H[i]*H[i]+sum[i-1]-2*H[i]*H[j]+H[j]*H[j]+DP[j]-sum[j])
			B[i]=j;
		}
	}
	
	for(int i=1;i<=n;i++)
	if(DP[i]!=dp[i])
	cout<<DP[i]<<" "<<dp[i]<<" "<<i<<" "<<B[i]<<endl;*/
	
	cout<<dp[n]<<endl;
}
/*
6
3 8 7 1 6 6
0 -1 9 1 2 0

20
587 7874 8557 8774 4337 4840 753 8130 8169 8291  1265 2647 7412 7414 7784 3118 4177 5906 6764 7214 
587 7874 8557 8774 4337 4840 753 8130 8169 8291  1265 2647 7412 7414 7784 3118 4177 5906 6764 7214
*/


Compilation message

building.cpp:110:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main()
      ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 500 KB Output is correct
3 Correct 2 ms 500 KB Output is correct
4 Correct 3 ms 500 KB Output is correct
5 Correct 2 ms 608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 80 ms 3820 KB Output is correct
2 Correct 80 ms 4972 KB Output is correct
3 Correct 94 ms 6040 KB Output is correct
4 Correct 80 ms 6676 KB Output is correct
5 Correct 62 ms 9316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 500 KB Output is correct
3 Correct 2 ms 500 KB Output is correct
4 Correct 3 ms 500 KB Output is correct
5 Correct 2 ms 608 KB Output is correct
6 Correct 80 ms 3820 KB Output is correct
7 Correct 80 ms 4972 KB Output is correct
8 Correct 94 ms 6040 KB Output is correct
9 Correct 80 ms 6676 KB Output is correct
10 Correct 62 ms 9316 KB Output is correct
11 Correct 77 ms 9316 KB Output is correct
12 Correct 81 ms 10272 KB Output is correct
13 Correct 61 ms 10940 KB Output is correct
14 Correct 82 ms 12312 KB Output is correct
15 Correct 73 ms 17628 KB Output is correct
16 Correct 69 ms 17628 KB Output is correct
17 Correct 28 ms 17628 KB Output is correct
18 Correct 26 ms 17628 KB Output is correct