結果

問題 No.952 危険な火薬庫
ユーザー chocoruskchocorusk
提出日時 2019-12-15 00:54:10
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 213 ms / 2,000 ms
コード長 2,601 bytes
コンパイル時間 1,158 ms
コンパイル使用メモリ 118,284 KB
実行使用メモリ 76,172 KB
最終ジャッジ日時 2023-09-10 18:17:51
合計ジャッジ時間 4,117 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 145 ms
62,580 KB
testcase_04 AC 141 ms
60,996 KB
testcase_05 AC 113 ms
56,292 KB
testcase_06 AC 170 ms
68,852 KB
testcase_07 AC 61 ms
41,500 KB
testcase_08 AC 194 ms
73,384 KB
testcase_09 AC 199 ms
73,332 KB
testcase_10 AC 80 ms
47,944 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 84 ms
47,896 KB
testcase_13 AC 87 ms
48,084 KB
testcase_14 AC 24 ms
26,684 KB
testcase_15 AC 127 ms
58,892 KB
testcase_16 AC 29 ms
27,032 KB
testcase_17 AC 24 ms
24,888 KB
testcase_18 AC 39 ms
33,512 KB
testcase_19 AC 3 ms
5,992 KB
testcase_20 AC 3 ms
7,676 KB
testcase_21 AC 76 ms
45,728 KB
testcase_22 AC 20 ms
23,080 KB
testcase_23 AC 3 ms
7,996 KB
testcase_24 AC 10 ms
16,328 KB
testcase_25 AC 213 ms
76,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef __int128_t lll;
template<typename T, bool ismin=true>
struct ConvexHullTrickMonotone{
	using P=pair<T, T>;
	deque<P> deq;
	ConvexHullTrickMonotone(){}
	void add(T a, T b){
		if(!ismin) a*=(-1), b*=(-1);
		if(deq.empty()){
			deq.push_back({a, b});
			return;
		}
		if(deq.back().first>=a){
			if(deq.back().first==a && deq.back().second<=b) return;
			while(deq.size()>1){
				T a1=deq.back().first, b1=deq.back().second;
				T a2=deq[deq.size()-2].first, b2=deq[deq.size()-2].second;
				if((b1-b2)*(a1-a)<(b-b1)*(a2-a1)) break;
				deq.pop_back();
			}
			deq.push_back({a, b});
		}else{
			while(deq.size()>1){
				T a1=deq.front().first, b1=deq.front().second;
				T a2=deq[1].first, b2=deq[1].second;
				if((b1-b2)*(a1-a)>(b-b1)*(a2-a1)) break;
				deq.pop_front();
			}
			deq.push_front({a, b});
		}
	}
	T get(T x, bool isinc){
		if(isinc){
			while(deq.size()>1){
				T a1=deq.front().first, b1=deq.front().second;
				T a2=deq[1].first, b2=deq[1].second;
				if(a1*x+b1<a2*x+b2) break;
				deq.pop_front();
			}
			T ret=deq.front().first*x+deq.front().second;
			if(ismin) return ret;
			else return -ret;
		}else{
			while(deq.size()>1){
				T a1=deq.back().first, b1=deq.back().second;
				T a2=deq[deq.size()-2].first, b2=deq[deq.size()-2].second;
				if(a1*x+b1<a2*x+b2) break;
				deq.pop_back();
			}
			T ret=deq.back().first*x+deq.back().second;
			if(ismin) return ret;
			else return -ret;
		}
	}
	void clear(){
		deq.clear();
	}
};
int n;
ll a[3030];
ll s[3030];
int main()
{
	cin>>n;
	for(int i=1; i<=n; i++){
		cin>>a[i];
		s[i]=s[i-1]+a[i];
	}
	vector<ConvexHullTrickMonotone<lll>> cht(n+2);
	ll dp[3030][3030];
	for(int i=0; i<=n+1; i++) for(int j=0; j<i; j++) dp[i][j]=1e18;
	for(int i=1; i<=n+1; i++){
		dp[i][0]=0;
		for(int j=1; j<i; j++){
			dp[i][j]=cht[i-j-1].get(s[i-1], true)+s[i-1]*s[i-1];
			if(j<i-1) dp[i][j]=min(dp[i][j], dp[i-1][j]);
		}
		cht[i].add(-2*s[i], s[i]*s[i]);
		for(int j=1; j<i; j++){
			cht[i-j].add(-2*s[i], s[i]*s[i]+dp[i][j]);
		}
	}
	for(int i=1; i<=n; i++) cout<<dp[n+1][i]<<endl;
	return 0;
}
0