結果

問題 No.817 Coin donation
ユーザー けーむけーむ
提出日時 2020-05-12 01:26:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 196 ms / 2,000 ms
コード長 1,926 bytes
コンパイル時間 1,756 ms
コンパイル使用メモリ 174,696 KB
実行使用メモリ 18,916 KB
最終ジャッジ日時 2023-09-26 19:07:29
合計ジャッジ時間 3,431 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 5 ms
4,376 KB
testcase_06 AC 18 ms
5,184 KB
testcase_07 AC 23 ms
5,880 KB
testcase_08 AC 115 ms
14,052 KB
testcase_09 AC 27 ms
6,196 KB
testcase_10 AC 196 ms
18,648 KB
testcase_11 AC 196 ms
18,848 KB
testcase_12 AC 180 ms
18,916 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"

ll n, k;
vector<ll> a, b;
vector<ll> pat, dp;

ll get_index(ll val) {
    return lower_bound(all(pat), val) - pat.begin();
}

ll get_count(ll val) {
    ll ans = 0;
    ll pc = sz(pat);
    rep(i, pc) {
        if (val < pat[i]) break;
        if (i == (pc - 1)) {
            ans += dp[i * 2];
            break;
        }
        ans += dp[i * 2];
        if (pat[i + 1] <= val) {
            ll d = pat[i + 1] - pat[i] - 1;
            ans += dp[i * 2 + 1] * d;
        }
        else {
            ll d = val - pat[i];
            ans += dp[i * 2 + 1] * d;
        }
    }
    return ans;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    cin >> n >> k;
    a.resize(n);
    b.resize(n);
    rep(i, n) cin >> a[i] >> b[i];
    set<ll> tmp;
    rep(i, n) {
        tmp.insert(a[i]);
        tmp.insert(b[i]);
    }
    for(auto x : tmp) pat.push_back(x);
    dp.resize(2 * sz(pat), 0);
    rep(i, n) {
        ll lidx = get_index(a[i]);
        ll ridx = get_index(b[i]);
        dp[lidx * 2]++;
        dp[ridx * 2 + 1]--;
    }
    rep(i, 2 * sz(pat)) dp[i + 1] += dp[i];
    ll low = 0, high = LONG_LONG_MAX;
    while((low + 1) < high) {
        ll mid = (low + high) / 2;
        ll cnt = get_count(mid);
        if (cnt >= k) {
            high = mid;
        }
        else {
            low = mid;
        }
    }
    cout << high << endl;
    return 0;
}
0