답안 #319599

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
319599 2020-11-05T17:48:21 Z kshitij_sodani Building Bridges (CEOI17_building) C++14
100 / 100
241 ms 9320 KB
#include <bits/stdc++.h>
using namespace std;
typedef int64_t llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
llo n;
vector<pair<llo,llo>> cht;
vector<pair<llo,llo>> cur;
vector<pair<llo,llo>> cht2;
vector<pair<llo,llo>> cht3;
llo bl=350;
llo inf=1e19;
llo val(pair<llo,llo> x,llo y){
	return x.a*y+x.b;
}
bool cmp(pair<llo,llo> x,pair<llo,llo> y){
	if(x.a<y.a){
		return 1;
	}
	else if(y.a<x.a){
		return 0;
	}
	return x.b>y.b;
}
void update(){
	if(cur.size()>bl){
		
		sort(cur.begin(),cur.end());
		reverse(cur.begin(),cur.end());
		llo ind=0;
		llo ind2=0;
		while(ind<cur.size() and ind2<cht.size()){
			if(cur[ind].a>cht[ind2].a or  (cur[ind].a==cht[ind2].a and cur[ind].b>cht[ind2].b )){
				cht2.pb(cur[ind]);
				ind+=1;
			}
			else{
				cht2.pb({cht[ind2]});
				ind2+=1;
			}
		}
		while(ind<cur.size() ){
			cht2.pb(cur[ind]);
			ind+=1;
		}
		while(ind2<cht.size() ){
			cht2.pb(cht[ind2]);
			ind2+=1;
		}
		cht.clear();
		cur.clear();
 
		for(auto j:cht2){
			while(cht.size()>1){
				if(cht[cht.size()-1].a==j.a){
					cht.pop_back();
					continue;
				}
				
				if((cht[cht.size()-1].a-cht[cht.size()-2].a)*(cht[cht.size()-1].b-j.b)<=(-cht[cht.size()-1].a+j.a)*(cht[cht.size()-2].b-cht[cht.size()-1].b)){
					cht.pop_back();
				}	
				else{
					break;
				}
			}
			cht.pb(j);
		}
		cht2.clear();
		cht3.clear();
		cht3.pb({-inf,-inf});
		for(int j=1;j<cht.size();j++){
			cht3.pb({cht[j].b-cht[j-1].b,cht[j-1].a-cht[j].a});
			if(cht3.back().b<0){
				cht3.back().a=-cht3.back().a;
				cht3.back().b=-cht3.back().b;
 
			}
		}
	}
}
void insert(llo aa,llo bb){
	cur.pb({aa,bb});
	update();
}
 
llo bin(llo x){
	llo low=0;
	llo high=cht.size()-1;
	llo ans=inf;
	while(low<high-1){
		int mid=(low+high)/2;
		ans=min(ans,val(cht[mid],x));
		if(x*cht3[mid].b>=cht3[mid].a){
			low=mid;
		}
		else{
			high=mid;
		}
	}
	for(llo i=low;i<high+1;i++){
		ans=min(ans,val(cht[i],x));
	}
	return ans;
}
llo query(llo x){
	llo mi=inf;
	for(auto j:cur){
		mi=min(mi,val(j,x));
	}
	mi=min(mi,bin(x));
	return mi;
}
llo aa[100001];
llo bb[100001];
llo dp[100001];
llo pre[100001];
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n;
	for(llo i=0;i<n;i++){
		cin>>aa[i];
	}
	for(llo i=0;i<n;i++){
		cin>>bb[i];
	}
	pre[0]=bb[0];
	for(llo i=1;i<n;i++){
		pre[i]=pre[i-1]+bb[i];
	}
	dp[0]=0;
	insert(-2*aa[0],dp[0]+aa[0]*aa[0]-pre[0]);
	for(llo i=1;i<n;i++){
		dp[i]=query(aa[i])+pre[i-1]+aa[i]*aa[i];
		insert(-2*aa[i],dp[i]+aa[i]*aa[i]-pre[i]);
		
	}
	cout<<dp[n-1]<<endl;
 
 
 
 
 
	return 0;
}

Compilation message

building.cpp:14:9: warning: overflow in conversion from 'double' to 'llo' {aka 'long int'} changes value from '1.0e+19' to '9223372036854775807' [-Woverflow]
   14 | llo inf=1e19;
      |         ^~~~
building.cpp: In function 'void update()':
building.cpp:28:15: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long int, long int> >::size_type' {aka 'long unsigned int'} and 'llo' {aka 'long int'} [-Wsign-compare]
   28 |  if(cur.size()>bl){
      |     ~~~~~~~~~~^~~
building.cpp:34:12: warning: comparison of integer expressions of different signedness: 'llo' {aka 'long int'} and 'std::vector<std::pair<long int, long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |   while(ind<cur.size() and ind2<cht.size()){
      |         ~~~^~~~~~~~~~~
building.cpp:34:32: warning: comparison of integer expressions of different signedness: 'llo' {aka 'long int'} and 'std::vector<std::pair<long int, long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |   while(ind<cur.size() and ind2<cht.size()){
      |                            ~~~~^~~~~~~~~~~
building.cpp:44:12: warning: comparison of integer expressions of different signedness: 'llo' {aka 'long int'} and 'std::vector<std::pair<long int, long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |   while(ind<cur.size() ){
      |         ~~~^~~~~~~~~~~
building.cpp:48:13: warning: comparison of integer expressions of different signedness: 'llo' {aka 'long int'} and 'std::vector<std::pair<long int, long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |   while(ind2<cht.size() ){
      |         ~~~~^~~~~~~~~~~
building.cpp:74:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long int, long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |   for(int j=1;j<cht.size();j++){
      |               ~^~~~~~~~~~~
# 결과 실행 시간 메모리 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 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 3556 KB Output is correct
2 Correct 61 ms 4580 KB Output is correct
3 Correct 62 ms 4708 KB Output is correct
4 Correct 56 ms 4452 KB Output is correct
5 Correct 100 ms 5464 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 71 ms 3556 KB Output is correct
7 Correct 61 ms 4580 KB Output is correct
8 Correct 62 ms 4708 KB Output is correct
9 Correct 56 ms 4452 KB Output is correct
10 Correct 100 ms 5464 KB Output is correct
11 Correct 59 ms 4708 KB Output is correct
12 Correct 63 ms 4728 KB Output is correct
13 Correct 52 ms 4580 KB Output is correct
14 Correct 63 ms 4836 KB Output is correct
15 Correct 241 ms 9320 KB Output is correct
16 Correct 98 ms 5588 KB Output is correct
17 Correct 46 ms 4580 KB Output is correct
18 Correct 44 ms 4580 KB Output is correct