結果

問題 No.817 Coin donation
ユーザー @abcde@abcde
提出日時 2019-05-19 22:36:51
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 1,485 bytes
コンパイル時間 1,680 ms
コンパイル使用メモリ 174,504 KB
実行使用メモリ 5,372 KB
最終ジャッジ日時 2023-10-17 08:00:18
合計ジャッジ時間 2,917 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 4 ms
4,348 KB
testcase_06 AC 14 ms
4,348 KB
testcase_07 AC 17 ms
4,348 KB
testcase_08 AC 64 ms
5,372 KB
testcase_09 AC 20 ms
4,348 KB
testcase_10 AC 91 ms
5,372 KB
testcase_11 AC 91 ms
5,372 KB
testcase_12 AC 91 ms
5,372 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// TODO: 高速化.
#include <bits/stdc++.h>
using namespace std;
using LL = long long;

int main() {
    
    // 1. 入力情報取得.
    LL N, K;
    cin >> N >> K;
    
    // 2. 硬貨準備.
    vector<pair<LL, LL>> v;
    LL upper = 0;
    for(int i = 0; i < N; i++){
        LL a, b;
        cin >> a >> b;
        upper = max(upper, b);
        v.push_back(make_pair(a, b));
    }
    
    // 3. 昇順sort.
    sort(v.begin(), v.end());
    
    // 4. K番目を探索.
    LL ans = 0;
    // int counter = 0;
    LL l = -1, r = upper;
    while(1){
        
        // 4-1. 探索.
        // 今回調査する硬貨の金額.
        LL cur = (l + r) / 2;
        LL sIndex = 0, eIndex = 0;
        for(auto &p : v){
            if(p.first > cur) break;
            LL cur1 = min(cur - 1, p.second);
            LL cur2 = min(cur,     p.second);
            sIndex += cur1 - p.first + 1;
            eIndex += cur2 - p.first + 1;
        }
        
        // 4-2. 終了条件チェック.
        // 4-2-1. K番目に安い硬貨が, 見つかった場合.
        if(sIndex + 1 <= K && K <= eIndex){
            ans = cur;
            break;
        }
        // 4-2-2. K番目に安い硬貨が, 見つからなかった場合.
        if(K < sIndex + 1) r = cur;
        if(K > eIndex)     l = cur;
        
        // debug.
        // if(counter > 500) break;
        // counter++;
        
    }

    // 5. 後処理.
    cout << ans << endl;
    return 0;
    
}
0