結果

問題 No.913 木の燃やし方
ユーザー 👑 kmjpkmjp
提出日時 2019-10-19 02:19:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 626 ms / 3,000 ms
コード長 2,253 bytes
コンパイル時間 1,730 ms
コンパイル使用メモリ 173,596 KB
実行使用メモリ 11,156 KB
最終ジャッジ日時 2023-09-08 08:06:20
合計ジャッジ時間 19,831 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,452 KB
testcase_01 AC 2 ms
5,400 KB
testcase_02 AC 2 ms
5,468 KB
testcase_03 AC 7 ms
5,536 KB
testcase_04 AC 7 ms
5,496 KB
testcase_05 AC 7 ms
5,496 KB
testcase_06 AC 6 ms
5,520 KB
testcase_07 AC 4 ms
5,468 KB
testcase_08 AC 427 ms
9,484 KB
testcase_09 AC 405 ms
9,288 KB
testcase_10 AC 412 ms
9,464 KB
testcase_11 AC 418 ms
9,368 KB
testcase_12 AC 441 ms
9,616 KB
testcase_13 AC 416 ms
9,480 KB
testcase_14 AC 392 ms
9,316 KB
testcase_15 AC 389 ms
9,308 KB
testcase_16 AC 407 ms
9,544 KB
testcase_17 AC 418 ms
9,332 KB
testcase_18 AC 415 ms
9,304 KB
testcase_19 AC 626 ms
11,104 KB
testcase_20 AC 428 ms
9,908 KB
testcase_21 AC 489 ms
9,688 KB
testcase_22 AC 522 ms
9,752 KB
testcase_23 AC 576 ms
9,804 KB
testcase_24 AC 579 ms
10,060 KB
testcase_25 AC 591 ms
11,156 KB
testcase_26 AC 442 ms
9,708 KB
testcase_27 AC 621 ms
10,988 KB
testcase_28 AC 432 ms
9,712 KB
testcase_29 AC 425 ms
9,596 KB
testcase_30 AC 448 ms
9,772 KB
testcase_31 AC 486 ms
10,900 KB
testcase_32 AC 476 ms
10,860 KB
testcase_33 AC 475 ms
10,808 KB
testcase_34 AC 471 ms
9,760 KB
testcase_35 AC 480 ms
9,624 KB
testcase_36 AC 472 ms
9,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------


int N;
ll A[202020];
ll S[202020];
ll ret[202020];
ll cand[202020];

template<typename V> struct ConvexHull {
	deque<pair<V,V>> Q;
	V calc(pair<V,V> p, V x) {
		return p.first*x+p.second;
	}
	int dodo(pair<V,V> A,pair<V,V> B, pair<V,V> C) { // max or min
		return ((__int128)(C.second-A.second)*(A.first-B.first)<=(__int128)(B.second-A.second)*(A.first-C.first));
	}
	void add(V a, V b) { // add ax+b
		Q.push_back({a,b});
		int v;
		while((v=Q.size())>=3 && dodo(Q[v-3],Q[v-2],Q[v-1]))
			Q[v-2]=Q[v-1], Q.pop_back();
	}
	V query(V x) {
		int L=-1,R=Q.size()-1;
		while(R-L>1) {
			int M=(L+R)/2;
			(((calc(Q[M],x)>=calc(Q[M+1],x)))?L:R)=M;
		}
		return calc(Q[R],x);
	}
};

void dfs(int L,int R) {
	if(R-L<=2) {
		if(R-L==2) {
			ret[L]=min(ret[L],4+A[L]+A[L+1]);
			ret[L+1]=min(ret[L+1],4+A[L]+A[L+1]);
		}
		return;
	}
	int M=(L+R)/2;
	dfs(L,M);
	dfs(M+1,R);
	int i;
	ConvexHull<ll> ch;
	for(i=L;i<=M;i++) cand[i]=1LL<<60;
	for(i=M+1;i<=R;i++) {
		ch.add(-2*i,1LL*i*i+S[i]);
	}
	for(i=M;i>=L;i--) {
		cand[i]=1LL*i*i-S[i]+ch.query(i);
	}
	for(i=L;i<=M;i++) {
		cand[i+1]=min(cand[i+1],cand[i]);
		ret[i]=min(ret[i],cand[i]);
	}
	ch.Q.clear();
	for(i=M;i<=R;i++) cand[i]=1LL<<60;
	for(i=L;i<=M;i++) {
		ch.add(-2*i,1LL*i*i-S[i]);
	}
	for(i=M;i<R;i++) {
		cand[i]=1LL*(i+1)*(i+1)+S[i+1]+ch.query(i+1);
	}
	for(i=R-1;i>=M;i--) {
		cand[i-1]=min(cand[i],cand[i-1]);
		ret[i]=min(ret[i],cand[i]);
	}
	
	
	
	
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	FOR(i,N) {
		cin>>A[i];
		S[i+1]=S[i]+A[i];
		ret[i]=1+A[i];
	}
	
	dfs(0,N);
	FOR(i,N) cout<<ret[i]<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0