結果
| 問題 |
No.952 危険な火薬庫
|
| コンテスト | |
| ユーザー |
kmjp
|
| 提出日時 | 2019-12-15 00:14:24 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 598 ms / 2,000 ms |
| コード長 | 1,731 bytes |
| コンパイル時間 | 1,737 ms |
| コンパイル使用メモリ | 174,288 KB |
| 実行使用メモリ | 74,496 KB |
| 最終ジャッジ日時 | 2024-06-28 04:07:04 |
| 合計ジャッジ時間 | 7,477 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 23 |
ソースコード
#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;
}
kmjp