結果

問題 No.952 危険な火薬庫
ユーザー 👑 kmjpkmjp
提出日時 2019-12-15 00:14:24
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 572 ms / 2,000 ms
コード長 1,731 bytes
コンパイル時間 1,753 ms
コンパイル使用メモリ 171,916 KB
実行使用メモリ 75,124 KB
最終ジャッジ日時 2023-09-10 12:48:58
合計ジャッジ時間 7,794 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 386 ms
62,752 KB
testcase_04 AC 377 ms
60,688 KB
testcase_05 AC 301 ms
56,688 KB
testcase_06 AC 463 ms
66,904 KB
testcase_07 AC 149 ms
39,604 KB
testcase_08 AC 533 ms
71,004 KB
testcase_09 AC 541 ms
73,148 KB
testcase_10 AC 203 ms
45,876 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 217 ms
47,952 KB
testcase_13 AC 226 ms
48,036 KB
testcase_14 AC 49 ms
24,820 KB
testcase_15 AC 338 ms
58,556 KB
testcase_16 AC 65 ms
26,904 KB
testcase_17 AC 50 ms
24,828 KB
testcase_18 AC 91 ms
31,172 KB
testcase_19 AC 3 ms
5,896 KB
testcase_20 AC 3 ms
6,104 KB
testcase_21 AC 194 ms
45,904 KB
testcase_22 AC 39 ms
22,660 KB
testcase_23 AC 4 ms
5,936 KB
testcase_24 AC 17 ms
14,316 KB
testcase_25 AC 572 ms
75,124 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))
//-------------------------------------------------------

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)(A.second-C.second)*(B.first-A.first)<=(__int128)(A.second-B.second)*(C.first-A.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);
	}
};

int N;
int A[3030];
ll S[3030];
ll cost[3030][3030];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	FOR(i,N) {
		cin>>A[i+1];
		S[i+1]=S[i]+A[i+1];
	}
	
	FOR(x,N+2) FOR(y,N+2) cost[x][y]=1LL<<60;
	cost[0][0]=0;
	for(i=1;i<=N+1;i++) {
		ConvexHull<ll> ch;
		for(x=i;x<=N+1;x++) {
			ch.add(-2*S[x-1],cost[i-1][x-1]+S[x-1]*S[x-1]);
			cost[i][x]=ch.query(S[x-1])+S[x-1]*S[x-1];
		}
	}
	
	for(i=1;i<=N;i++) cout<<cost[N+1-i][N+1]<<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