結果

問題 No.705 ゴミ拾い Hard
ユーザー tsutajtsutaj
提出日時 2018-06-16 03:49:04
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 295 ms / 1,500 ms
コード長 2,862 bytes
コンパイル時間 2,206 ms
コンパイル使用メモリ 107,848 KB
実行使用メモリ 12,872 KB
最終ジャッジ日時 2023-09-13 05:50:44
合計ジャッジ時間 7,844 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,488 KB
testcase_01 AC 2 ms
9,472 KB
testcase_02 AC 2 ms
9,600 KB
testcase_03 AC 3 ms
9,648 KB
testcase_04 AC 2 ms
9,464 KB
testcase_05 AC 3 ms
9,488 KB
testcase_06 AC 2 ms
9,524 KB
testcase_07 AC 2 ms
9,516 KB
testcase_08 AC 3 ms
9,468 KB
testcase_09 AC 2 ms
9,716 KB
testcase_10 AC 3 ms
9,516 KB
testcase_11 AC 2 ms
9,548 KB
testcase_12 AC 2 ms
9,484 KB
testcase_13 AC 3 ms
9,488 KB
testcase_14 AC 3 ms
9,712 KB
testcase_15 AC 3 ms
9,488 KB
testcase_16 AC 3 ms
9,600 KB
testcase_17 AC 4 ms
9,492 KB
testcase_18 AC 3 ms
9,480 KB
testcase_19 AC 4 ms
9,472 KB
testcase_20 AC 3 ms
9,480 KB
testcase_21 AC 3 ms
9,476 KB
testcase_22 AC 2 ms
9,548 KB
testcase_23 AC 3 ms
9,744 KB
testcase_24 AC 285 ms
12,708 KB
testcase_25 AC 285 ms
12,872 KB
testcase_26 AC 286 ms
12,708 KB
testcase_27 AC 285 ms
12,712 KB
testcase_28 AC 289 ms
12,708 KB
testcase_29 AC 289 ms
12,748 KB
testcase_30 AC 277 ms
12,712 KB
testcase_31 AC 254 ms
12,708 KB
testcase_32 AC 266 ms
12,708 KB
testcase_33 AC 131 ms
12,716 KB
testcase_34 AC 131 ms
12,748 KB
testcase_35 AC 208 ms
12,704 KB
testcase_36 AC 245 ms
12,776 KB
testcase_37 AC 210 ms
12,692 KB
testcase_38 AC 244 ms
12,816 KB
testcase_39 AC 255 ms
12,700 KB
testcase_40 AC 3 ms
9,472 KB
testcase_41 AC 3 ms
9,524 KB
testcase_42 AC 2 ms
9,600 KB
testcase_43 AC 295 ms
12,712 KB
権限があれば一括ダウンロードができます

ソースコード

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;
}
0