結果

問題 No.704 ゴミ拾い Medium
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2018-06-16 08:24:09
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 306 ms / 1,500 ms
コード長 3,191 bytes
コンパイル時間 1,942 ms
コンパイル使用メモリ 179,560 KB
実行使用メモリ 51,116 KB
最終ジャッジ日時 2023-09-13 05:57:17
合計ジャッジ時間 10,065 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
41,932 KB
testcase_01 AC 12 ms
41,976 KB
testcase_02 AC 13 ms
41,792 KB
testcase_03 AC 13 ms
41,800 KB
testcase_04 AC 12 ms
41,900 KB
testcase_05 AC 13 ms
41,920 KB
testcase_06 AC 12 ms
41,904 KB
testcase_07 AC 12 ms
41,804 KB
testcase_08 AC 13 ms
41,804 KB
testcase_09 AC 12 ms
41,860 KB
testcase_10 AC 13 ms
41,860 KB
testcase_11 AC 13 ms
41,924 KB
testcase_12 AC 12 ms
41,900 KB
testcase_13 AC 13 ms
41,968 KB
testcase_14 AC 14 ms
41,928 KB
testcase_15 AC 13 ms
41,984 KB
testcase_16 AC 14 ms
41,792 KB
testcase_17 AC 13 ms
41,960 KB
testcase_18 AC 14 ms
41,976 KB
testcase_19 AC 14 ms
41,924 KB
testcase_20 AC 13 ms
41,976 KB
testcase_21 AC 14 ms
41,968 KB
testcase_22 AC 14 ms
42,032 KB
testcase_23 AC 13 ms
41,960 KB
testcase_24 AC 303 ms
50,220 KB
testcase_25 AC 306 ms
50,352 KB
testcase_26 AC 306 ms
51,116 KB
testcase_27 AC 304 ms
50,144 KB
testcase_28 AC 302 ms
49,556 KB
testcase_29 AC 303 ms
50,356 KB
testcase_30 AC 305 ms
49,632 KB
testcase_31 AC 302 ms
49,820 KB
testcase_32 AC 301 ms
49,632 KB
testcase_33 AC 305 ms
49,536 KB
testcase_34 AC 207 ms
50,344 KB
testcase_35 AC 206 ms
50,680 KB
testcase_36 AC 210 ms
49,696 KB
testcase_37 AC 207 ms
50,612 KB
testcase_38 AC 206 ms
50,092 KB
testcase_39 AC 207 ms
50,416 KB
testcase_40 AC 206 ms
50,600 KB
testcase_41 AC 206 ms
50,148 KB
testcase_42 AC 207 ms
50,604 KB
testcase_43 AC 208 ms
49,796 KB
testcase_44 AC 13 ms
41,976 KB
testcase_45 AC 13 ms
41,800 KB
testcase_46 AC 194 ms
50,056 KB
testcase_47 AC 222 ms
50,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
#define def infl
template<class V, int NV> struct SegTree { //[l,r)
    V comp(V& l, V& r) { return min(l,r); };
    vector<V> val; SegTree() { val = vector<V>(NV * 2, def); }
    V get(int x,int y,int l=0,int r=NV,int k=1){if(r<=x||y<=l)return def;if(x<=l&&r<=y)return val[k];
    auto a=get(x,y,l,(l+r)/2,k*2);auto b=get(x,y,(l+r)/2,r,k*2+1);return comp(a,b);}
    void update(int i, V v) { i+=NV;val[i]=v;while(i>1)i>>=1,val[i]=comp(val[i*2],val[i*2+1]); }
    void add(int i, V v) { update(i, val[i + NV] + v); }
    V operator[](int x) { return get(x, x + 1); }};
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/






int N;
ll A[301010], X[301010], Y[301010];
ll dp[301010];
SegTree<ll, 1 << 20> st1, st2;
vector<int> dic;
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N;
    rep(i, 1, N + 1) cin >> A[i];
    rep(i, 1, N + 1) cin >> X[i];
    rep(i, 1, N + 1) cin >> Y[i];

    rep(i, 0, N + 1) dp[i] = infl;
    dp[0] = 0;
    
    /* O(N^2) ver1
    rep(i, 1, N + 1) {
        rep(j, 0, i) {
            ll dx = abs(A[i] - X[j + 1]);
            ll dy = Y[j + 1];
            ll z = dp[j] + dx + dy;
            chmin(dp[i], z);
        }
    }*/

    /* O(N^2) ver2
    rep(i, 1, N + 1) {
        rep(j, 0, i) if (A[i] < X[j + 1]) chmin(dp[i], dp[j] + X[j + 1] - A[i] + Y[j + 1]);
        rep(j, 0, i) if (A[i] >= X[j + 1]) chmin(dp[i], dp[j] + A[i] - X[j + 1] + Y[j + 1]);
    }*/

    rep(i, 1, N + 1) dic.push_back(A[i]);
    rep(i, 1, N + 1) dic.push_back(X[i]);
    sort(all(dic));
    dic.erase(unique(all(dic)), dic.end());

    rep(i, 1, N + 1) {
        int x = lower_bound(all(dic), X[i]) - dic.begin();
        int a = lower_bound(all(dic), A[i]) - dic.begin();

        st1.update(x, min(st1[x], dp[i - 1] + X[i] + Y[i]));
        st2.update(x, min(st2[x], dp[i - 1] - X[i] + Y[i]));

        chmin(dp[i], st1.get(a, 1 << 20) - A[i]);
        chmin(dp[i], st2.get(0, a) + A[i]);
    }

    cout << dp[N] << endl;
}
0