結果

問題 No.448 ゆきこーだーの雨と雪 (3)
ユーザー parukiparuki
提出日時 2016-11-18 23:33:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 4,289 bytes
コンパイル時間 1,547 ms
コンパイル使用メモリ 175,440 KB
実行使用メモリ 24,360 KB
最終ジャッジ日時 2024-06-11 19:31:19
合計ジャッジ時間 4,285 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,948 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 13 ms
6,940 KB
testcase_14 AC 14 ms
6,944 KB
testcase_15 AC 35 ms
11,272 KB
testcase_16 AC 58 ms
17,124 KB
testcase_17 AC 57 ms
18,248 KB
testcase_18 AC 11 ms
6,940 KB
testcase_19 AC 80 ms
22,280 KB
testcase_20 AC 27 ms
9,616 KB
testcase_21 AC 76 ms
22,444 KB
testcase_22 AC 28 ms
9,984 KB
testcase_23 AC 78 ms
22,472 KB
testcase_24 AC 20 ms
7,424 KB
testcase_25 AC 77 ms
22,400 KB
testcase_26 AC 10 ms
6,944 KB
testcase_27 AC 56 ms
17,324 KB
testcase_28 AC 57 ms
15,884 KB
testcase_29 AC 31 ms
10,512 KB
testcase_30 AC 83 ms
23,044 KB
testcase_31 AC 13 ms
6,944 KB
testcase_32 AC 12 ms
6,944 KB
testcase_33 AC 88 ms
24,192 KB
testcase_34 AC 84 ms
24,360 KB
testcase_35 AC 91 ms
24,272 KB
testcase_36 AC 78 ms
24,276 KB
testcase_37 AC 87 ms
24,360 KB
testcase_38 AC 80 ms
24,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define mt make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

template<class Cmp=less_equal<int>>
class SparseTableRMQ{
public:
    SparseTableRMQ(){
    }
    SparseTableRMQ(int *ar, int n): n(n), data(n){
        for(int i = 0; i < n;++i) data[i] = ar[i];
        table = buildRMQ();
    }

    int queryPos(int l, int r){
        int k = lgInt(r - l - 1);
        int lid = table[l + n*k], rid = table[r + n*k - (1 << k)];
        return (cmp(data[lid], data[rid])) ? lid : rid;
    }

    int queryValue(int l, int r){
        return data[queryPos(l, r)];
    }
private:
    int n;
    vector<int> data, table;
    Cmp cmp;
    int lgInt(int v){
        static const unsigned b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000};
        static const unsigned S[] = {1, 2, 4, 8, 16};
        int res = 0;
        for (int i = 4; i >= 0; i--) {
            if (v & b[i]) {
                v >>= S[i];
                res |= S[i];
            }
        }
        return res;
    }

    vector<int> buildRMQ() {
        int logn = 1;
        for (int k = 1; k < n; k *= 2) ++logn;
        vector<int> b(n*logn);
        for(int i = 0; i < n; ++i)b[i] = i;
        int cur = 0;
        for(int k = 1; k < n; k *= 2){
            copy(b.begin() + cur, b.begin() + cur + n, b.begin() + cur + n);
            cur += n;
            for(int i = 0; i < n - k; ++i){
                int idl = b[cur + i], idr = b[cur + i + k];
                if(cmp(data[idl], data[idr])){
                    b[cur + i] = idl;
                } else{
                    b[cur + i] = idr;
                }
            }
        }
        return b;
    }
};
typedef SparseTableRMQ<less_equal<int>> RMinQ;
typedef SparseTableRMQ<greater_equal<int>> RMaxQ;

int D[200001];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N, K;
    const ll INF = LLONG_MAX;
    while(cin >> N >> K) {
        vi T(N + 1);
        rep(i, N) cin >> T[i] >> D[i];

        // vector<pair<int, ll>> dp(N + 1, mp(INT_MAX, INF));
        
        vi dpA(N + 1, INT_MAX);
        vll dpB(N + 1, INF);

        dpA[0] = 0;
        dpB[0] = 0;
        // dp[0] = mp(0, 0);

        vll sumD(N + 1);
        rep(i, N) {
            sumD[i + 1] = sumD[i] + D[i];
        }
        RMaxQ rmq(D, N);

        T[N] = (int)2e9 + 1;

        /*
        rep(i, N) {
            smin(dp[i + 1], mp(max(dp[i].first, D[i]), dp[i].second + D[i]));
            
            int lb = i, ub = N, mid;
            while(ub - lb > 1) {
                mid = (lb + ub) / 2;
                (T[mid] - T[i] >= K ? ub : lb) = mid;
            }

            int x = i + 1 == ub ? 0 : rmq.queryValue(i + 1, ub);
            ll y = sumD[ub] - sumD[i + 1];
            
            smin(dp[ub], mp(max(dp[i].first, x), dp[i].second + y));
        }*/

        rep(i, N) {
            smin(dpA[i + 1], max(dpA[i], D[i]));
            int lb = i, ub = N, mid;
            while(ub - lb > 1) {
                mid = (lb + ub) / 2;
                (T[mid] - T[i] >= K ? ub : lb) = mid;
            }

            int x = i + 1 == ub ? 0 : rmq.queryValue(i + 1, ub);
            smin(dpA[ub], max(dpA[i], x));
        }

        int ansA = dpA[N];
        
        rep(i, N) {
            if(dpB[i] == INF)continue;

            if(D[i] <= ansA) {
                smin(dpB[i + 1], dpB[i] + D[i]);
            }

            int lb = i, ub = N, mid;
            while(ub - lb > 1) {
                mid = (lb + ub) / 2;
                (T[mid] - T[i] >= K ? ub : lb) = mid;
            }

            int x = i + 1 == ub ? 0 : rmq.queryValue(i + 1, ub);
            ll y = sumD[ub] - sumD[i + 1];
            if(x <= ansA) {
                smin(dpB[ub], dpB[i] + y);
            }
        }

        ll ansB = dpB[N];

        cout << ansA << endl;
        cout << ansB << endl;
    }
}
0