結果

問題 No.1715 Dinner 2
ユーザー tarattata1tarattata1
提出日時 2021-10-22 22:55:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 4,007 bytes
コンパイル時間 5,144 ms
コンパイル使用メモリ 174,540 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 14:21:54
合計ジャッジ時間 5,038 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 8 ms
4,348 KB
testcase_12 AC 6 ms
4,348 KB
testcase_13 AC 6 ms
4,348 KB
testcase_14 AC 6 ms
4,348 KB
testcase_15 AC 6 ms
4,348 KB
testcase_16 AC 8 ms
4,348 KB
testcase_17 AC 6 ms
4,348 KB
testcase_18 AC 4 ms
4,348 KB
testcase_19 AC 6 ms
4,348 KB
testcase_20 AC 6 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 5 ms
4,348 KB
testcase_23 AC 6 ms
4,348 KB
testcase_24 AC 5 ms
4,348 KB
testcase_25 AC 7 ms
4,348 KB
testcase_26 AC 6 ms
4,348 KB
testcase_27 AC 5 ms
4,348 KB
testcase_28 AC 6 ms
4,348 KB
testcase_29 AC 7 ms
4,348 KB
testcase_30 AC 6 ms
4,348 KB
testcase_31 AC 7 ms
4,348 KB
testcase_32 AC 8 ms
4,348 KB
testcase_33 AC 8 ms
4,348 KB
testcase_34 AC 8 ms
4,348 KB
testcase_35 AC 9 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 8 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <cassert>
#include <numeric>
#include <functional>
#include <ctime>
#include <bitset>
#pragma warning(disable:4996) 

#define ATCODER
#ifdef ATCODER
#include <atcoder/all>
#endif

typedef long long ll;
typedef unsigned long long ull;
#define LINF  9223300000000000000
#define LINF2 1223300000000000000
#define LINF3 1000000000000
#define INF 2140000000
//const long long MOD = 1000000007;
const long long MOD = 998244353;

using namespace std;
#ifdef ATCODER
using namespace atcoder;
#endif



void solve()
{
    int n, d;
    scanf("%d%d", &n, &d);
    vector<int> p(n), q(n), a(n);
    vector<pair<int, int>> z;   // p[i],i
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &p[i], &q[i]);
        a[i] = -p[i] + q[i];
        z.push_back(make_pair(p[i], i));
    }
    sort(z.begin(), z.end());

    vector<vector<int>> vv(n, vector<int>(3,-1));
    set<pair<int, int>> ss;
    for (int i = 0; i < n; i++) {
        int id = z[i].second;
        ss.insert(make_pair(a[id], id));
        auto it0 = ss.rbegin();
        vv[i][0]= it0->second;
        it0++;
        if (it0 == ss.rend()) {
            continue;
        }
        vv[i][1] = it0->second;
        it0++;
        if (it0 == ss.rend()) {
            continue;
        }
        vv[i][2] = it0->second;
    }

    int l = -1, r = 1000000000;
    while (r - l > 1) {
        int m = (l + r) / 2;
        int bad = 0;
        map<int, int> mp0;
        for (int i = 0; i < d; i++) {
            if (i == 0) {
                int val  = 0;
                int diff = m;
                int k = lower_bound(z.begin(), z.end(), make_pair(diff + 1, -INF))-z.begin();
                k--;
                if (k < 0) {
                    bad = 1;
                    break;
                }
                for (int aa = 0; aa < 3; aa++) {
                    if (vv[k][aa] >= 0) {
                        int idne = vv[k][aa];
                        int valne  = val + a[idne];
                        mp0[idne] = valne;
                    }
                }
                if (mp0.empty()) {
                    bad = 1;
                    break;
                }
            }
            else {
                map<int, int> mp;
                auto it0 = mp0.begin();
                for (; it0 != mp0.end(); ++it0) {
                    int val = it0->second;
                    int diff = m + val;
                    int k = lower_bound(z.begin(), z.end(), make_pair(diff + 1, -INF)) - z.begin();
                    k--;
                    if (k < 0) {
                        continue;
                    }
                    for (int aa = 0; aa < 3; aa++) {
                        if (vv[k][aa] >= 0) {
                            int id = it0->first;
                            int idne = vv[k][aa];
                            if (idne == id) continue;

                            int valne = val + a[idne];
                            auto it1 = mp.find(idne);
                            if (it1 == mp.end()) {
                                mp[idne] = valne;
                            }
                            else {
                                mp[idne] = max(mp[idne], valne);
                            }
                        }
                    }
                }
                if (mp.empty()) {
                    bad = 1;
                    break;
                }
                mp0.swap(mp);
            }
        }
        if (bad) {
            l = m;
        }
        else {
            r = m;
        }
    }
    printf("%d\n", -r);

    return;
}

int main()
{
#if 1
    solve();
#else
    int T, t;
    scanf("%d", &T);
    for (t = 0; t < T; t++) {
        //printf("Case #%d: ", t + 1);
        solve();
    }
#endif
    return 0;
}
0