結果
問題 | No.952 危険な火薬庫 |
ユーザー | rniya |
提出日時 | 2020-09-13 02:16:38 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 166 ms / 2,000 ms |
コード長 | 4,905 bytes |
コンパイル時間 | 1,991 ms |
コンパイル使用メモリ | 179,496 KB |
実行使用メモリ | 76,232 KB |
最終ジャッジ日時 | 2024-06-10 19:17:43 |
合計ジャッジ時間 | 4,211 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 119 ms
51,584 KB |
testcase_04 | AC | 117 ms
50,816 KB |
testcase_05 | AC | 91 ms
41,704 KB |
testcase_06 | AC | 139 ms
61,168 KB |
testcase_07 | AC | 50 ms
22,912 KB |
testcase_08 | AC | 163 ms
68,736 KB |
testcase_09 | AC | 161 ms
69,760 KB |
testcase_10 | AC | 66 ms
29,824 KB |
testcase_11 | AC | 2 ms
6,940 KB |
testcase_12 | AC | 68 ms
31,360 KB |
testcase_13 | AC | 72 ms
32,384 KB |
testcase_14 | AC | 17 ms
9,984 KB |
testcase_15 | AC | 104 ms
46,336 KB |
testcase_16 | AC | 21 ms
12,160 KB |
testcase_17 | AC | 17 ms
10,368 KB |
testcase_18 | AC | 31 ms
15,488 KB |
testcase_19 | AC | 2 ms
6,944 KB |
testcase_20 | AC | 3 ms
6,940 KB |
testcase_21 | AC | 62 ms
28,672 KB |
testcase_22 | AC | 13 ms
8,704 KB |
testcase_23 | AC | 2 ms
6,944 KB |
testcase_24 | AC | 7 ms
6,940 KB |
testcase_25 | AC | 166 ms
76,232 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; #define LOCAL #pragma region Macros typedef long long ll; #define ALL(x) (x).begin(),(x).end() const long long MOD=1000000007; // const long long MOD=998244353; const int INF=1e9; const long long IINF=1e18; const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1}; const char dir[4]={'D','R','U','L'}; template<typename T> istream &operator>>(istream &is,vector<T> &v){ for (T &x:v) is >> x; return is; } template<typename T> ostream &operator<<(ostream &os,const vector<T> &v){ for (int i=0;i<v.size();++i){ os << v[i] << (i+1==v.size()?"": " "); } return os; } template<typename T,typename U> ostream &operator<<(ostream &os,const pair<T,U> &p){ os << '(' << p.first << ',' << p.second << ')'; return os; } template<typename T,typename U> ostream &operator<<(ostream &os,const map<T,U> &m){ os << '{'; for (auto itr=m.begin();itr!=m.end();++itr){ os << '(' << itr->first << ',' << itr->second << ')'; if (++itr!=m.end()) os << ','; --itr; } os << '}'; return os; } template<typename T> ostream &operator<<(ostream &os,const set<T> &s){ os << '{'; for (auto itr=s.begin();itr!=s.end();++itr){ os << *itr; if (++itr!=s.end()) os << ','; --itr; } os << '}'; return os; } void debug_out(){cerr << '\n';} template<class Head,class... Tail> void debug_out(Head&& head,Tail&&... tail){ cerr << head; if (sizeof...(Tail)>0) cerr << ", "; debug_out(move(tail)...); } #ifdef LOCAL #define debug(...) cerr << " ";\ cerr << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << '\n';\ cerr << " ";\ debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif template<typename T> T gcd(T x,T y){return y!=0?gcd(y,x%y):x;} template<typename T> T lcm(T x,T y){return x/gcd(x,y)*y;} template<class T1,class T2> inline bool chmin(T1 &a,T2 b){ if (a>b){a=b; return true;} return false; } template<class T1,class T2> inline bool chmax(T1 &a,T2 b){ if (a<b){a=b; return true;} return false; } #pragma endregion template<typename T,bool isMin=true> struct ConvexHullTrick{ struct Line{ T a,b; Line(T a,T b):a(a),b(b){} }; deque<Line> Lines; bool empty() const {return Lines.empty();} inline int sgn(T a){return a==0?0:(a<0?-1:1);} inline bool check(const Line &a,const Line &b,const Line &c){ if (b.b==a.b||c.b==b.b) return sgn(b.a-a.a)*sgn(c.b-b.b)>=sgn(c.a-b.a)*sgn(b.b-a.b); return (long double)(b.a-a.a)*sgn(c.b-b.b)/(long double)abs(b.b-a.b)>=(long double)(c.a-b.a)*sgn(b.b-a.b)/(long double)abs(c.b-b.b); } void add(T a,T b){ if (!isMin) a*=-1,b*=-1; Line l(a,b); if (empty()){ Lines.emplace_back(l); return; } if (Lines.front().a<=a){ if (Lines.front().a==a){ if (Lines.front().b<=b) return; Lines.pop_front(); } while(Lines.size()>=2&&check(l,Lines.front(),Lines[1])) Lines.pop_front(); Lines.emplace_front(l); } else { if (Lines.back().a==a){ if (Lines.back().b<=b) return; Lines.pop_back(); } while(Lines.size()>=2&&check(Lines[Lines.size()-2],Lines.back(),l)) Lines.pop_back(); Lines.emplace_back(l); } } inline T f(const Line &l,const T &x){ return l.a*x+l.b; } T query(T x){ int lb=-1,ub=Lines.size()-1; while(ub-lb>1){ int mid=(ub+lb)>>1; (f(Lines[mid],x)>=f(Lines[mid+1],x)?lb:ub)=mid; } return (isMin?f(Lines[ub],x):-f(Lines[ub],x)); } T query_monotone_inc(T x){ while(Lines.size()>=2&&f(Lines.front(),x)>=f(Lines[1],x)) Lines.pop_front(); return (isMin?f(Lines.front(),x):-f(Lines.front(),x)); } T query_monotone_dec(T x){ while(Lines.size()>=2&&f(Lines.back(),x)>=f(Lines[Lines.size()-2],x)) Lines.pop_back(); return (isMin?f(Lines.back(),x):-f(Lines.back(),x)); } }; int main(){ cin.tie(0); ios::sync_with_stdio(false); const long long inf=(1LL<<62)-1; int N; cin >> N; vector<long long> A(N); for (int i=0;i<N;++i) cin >> A[i]; vector<long long> sum(N+1,0); for (int i=0;i<N;++i) sum[i+1]=sum[i]+A[i]; vector<ConvexHullTrick<long long>> CHT(N+2); vector<vector<long long>> dp(N+1,vector<long long>(N+1,IINF)); dp[0][0]=0; CHT[0].add(0,0); for (int i=0;i<N;++i){ for (int j=0;j<=i+1;++j){ dp[i+1][j]=min(dp[i+1][j],dp[i][j]); if (!CHT[i+1-j].empty()) dp[i+1][j]=min(dp[i+1][j],sum[i+1]*sum[i+1]+CHT[i+1-j].query_monotone_dec(-2*sum[i+1])); if (i<N-1) CHT[i+1-j].add(sum[i+1],dp[i][j]+sum[i+1]*sum[i+1]); } } for (int j=1;j<=N;++j) cout << dp[N][j] << '\n'; }