結果

問題 No.705 ゴミ拾い Hard
ユーザー 👑 kmjpkmjp
提出日時 2018-06-20 01:47:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 143 ms / 1,500 ms
コード長 2,130 bytes
コンパイル時間 1,848 ms
コンパイル使用メモリ 152,076 KB
実行使用メモリ 12,980 KB
最終ジャッジ日時 2023-09-13 07:18:32
合計ジャッジ時間 6,154 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,504 KB
testcase_01 AC 3 ms
9,456 KB
testcase_02 AC 3 ms
9,508 KB
testcase_03 AC 3 ms
9,568 KB
testcase_04 AC 3 ms
9,552 KB
testcase_05 AC 3 ms
9,420 KB
testcase_06 AC 3 ms
9,504 KB
testcase_07 AC 3 ms
9,444 KB
testcase_08 AC 3 ms
9,400 KB
testcase_09 AC 3 ms
9,592 KB
testcase_10 AC 3 ms
9,508 KB
testcase_11 AC 3 ms
9,588 KB
testcase_12 AC 3 ms
9,644 KB
testcase_13 AC 3 ms
9,648 KB
testcase_14 AC 3 ms
9,688 KB
testcase_15 AC 3 ms
9,536 KB
testcase_16 AC 3 ms
9,432 KB
testcase_17 AC 3 ms
9,412 KB
testcase_18 AC 3 ms
9,428 KB
testcase_19 AC 3 ms
9,432 KB
testcase_20 AC 3 ms
9,568 KB
testcase_21 AC 3 ms
9,520 KB
testcase_22 AC 3 ms
9,416 KB
testcase_23 AC 3 ms
9,412 KB
testcase_24 AC 142 ms
12,720 KB
testcase_25 AC 142 ms
12,856 KB
testcase_26 AC 142 ms
12,940 KB
testcase_27 AC 143 ms
12,844 KB
testcase_28 AC 141 ms
12,980 KB
testcase_29 AC 142 ms
12,704 KB
testcase_30 AC 139 ms
12,748 KB
testcase_31 AC 133 ms
12,816 KB
testcase_32 AC 135 ms
12,720 KB
testcase_33 AC 58 ms
12,808 KB
testcase_34 AC 58 ms
12,768 KB
testcase_35 AC 118 ms
12,872 KB
testcase_36 AC 130 ms
12,748 KB
testcase_37 AC 119 ms
12,840 KB
testcase_38 AC 129 ms
12,896 KB
testcase_39 AC 132 ms
12,812 KB
testcase_40 AC 3 ms
9,444 KB
testcase_41 AC 3 ms
9,444 KB
testcase_42 AC 3 ms
9,404 KB
testcase_43 AC 142 ms
12,728 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[303030];
ll X[303030];
ll Y[303030];
ll D[303030];

ll dist(int a,int b) {
	ll x=abs(A[b]-X[a]);
	ll y=abs(Y[a]);
	return x*x*x+y*y*y;
}

ll cost(int prev,int now) {
	return D[prev]+dist(prev+1,now);
}

int hoge(int L,int R) {
	// cost(L,m) >= cost(R,m)となる最小のm
	int TL=1, TR=N+1;
	while(TR-TL>1) {
		int TM=(TR+TL)/2;
		((cost(L,TM) >= cost(R,TM))?TR:TL)=TM;
	}
	return TR;
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	FOR(i,N) cin>>A[i+1];
	FOR(i,N) cin>>X[i+1];
	FOR(i,N) cin>>Y[i+1];
	
	deque<pair<int,int>> Q;
	Q.push_back({0,1});
	
	for(int now=1;now<=N;now++) {
		int pre=Q.back().first;
		
		//cout<<now<<" "<<pre<<" ";
		//FORR(q,Q) cout<<q.first<<":"<<q.second<<"  ";
		
		
		if(cost(pre,now) >= cost(now-1,now)) {
			//最新の値が最適
			D[now]=cost(now-1,now);
			Q.clear();
			Q.push_back({now-1,now+1});
		}
		else {
			//現状の最適値
			D[now]=cost(pre,now);
			//最新のケースの方がいい場合、それを先頭に追加していく
			while(cost(now-1,Q[0].second)<=cost(Q[0].first,Q[0].second)) Q.pop_front();
			//これまでの範囲でコストが反転する場所
			int h=hoge(Q[0].first,now-1);
			//hからコストが反転する
			if(h<=N) {
				Q.push_front({now-1,h});
			}
			//反転開始するなら末尾を葉ずつ
			if(Q.size()>=2 && Q[Q.size()-2].second==now+1) Q.pop_back();
			else Q.back().second++;
		}
		//cout<<D[now]<<endl;
	}
	
	cout<<D[N]<<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