結果

問題 No.705 ゴミ拾い Hard
ユーザー tsutaj
提出日時 2018-06-16 03:49:04
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 293 ms / 1,500 ms
コード長 2,862 bytes
コンパイル時間 1,045 ms
コンパイル使用メモリ 109,824 KB
実行使用メモリ 13,008 KB
最終ジャッジ日時 2024-06-30 16:01:23
合計ジャッジ時間 7,334 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

//
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
typedef pair<int, int> pii;
typedef long long ll;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;
const int S = 300010;
int N, X[S], Y[S], A[S], dp[S];
// l j
int w(int l, int j) {
int d1 = abs(A[j] - X[l]);
int d2 = abs(Y[l]);
return d1*d1*d1 + d2*d2*d2;
}
// dp[j] dp[j] = dp[l] + w(i+1, j)
int C(int l, int j) {
return dp[l] + w(l+1, j);
}
// k < l < N
// C(l, h) <= C(k, h) h
int bin_search(int k, int l) {
int ub = N+1, lb = l;
while(ub - lb > 1) {
int mid = (ub + lb) / 2;
if(C(l, mid) <= C(k, mid)) {
ub = mid;
}
else {
lb = mid;
}
}
return ub;
}
signed main() {
cin >> N;
for(int i=1; i<=N; i++) cin >> A[i];
for(int i=1; i<=N; i++) cin >> X[i];
for(int i=1; i<=N; i++) cin >> Y[i];
// (a, b) := dp[x] (x >= b) a
deque< pair<int, int> > deq;
deq.push_back(make_pair(0, 1));
for(int j=1; j<=N; j++) {
int l = deq.back().first;
//
if(C(j-1, j) <= C(l, j)) {
dp[j] = C(j-1, j);
deq.clear();
deq.push_front(make_pair(j-1, j+1));
}
else {
dp[j] = C(l, j);
while(C(j-1, deq[0].second) <= C(deq[0].first, deq[0].second)) deq.pop_front();
int h = bin_search(deq[0].first, j-1);
if(h != N+1) {
deq.push_front(make_pair(j-1, h));
}
if(deq.size() >= 2 && j+1 == deq[deq.size()-2].second) {
deq.pop_back();
}
else {
deq.back().second++;
}
}
}
cout << dp[N] << endl;
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0