結果

問題 No.507 ゲーム大会(チーム決め)
ユーザー milanis48663220
提出日時 2020-07-16 00:56:12
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 404 ms / 3,000 ms
コード長 1,257 bytes
コンパイル時間 1,258 ms
コンパイル使用メモリ 92,108 KB
実行使用メモリ 8,296 KB
最終ジャッジ日時 2024-11-22 19:23:51
合計ジャッジ時間 5,085 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;

int N, M;
int a;
vector<int> v;

bool ok(int idx){
    multiset<int> st;
    for(int i = 0; i < N-1; i++){
        if(i == idx) continue;
        st.insert(v[i]);
    }
    int cnt = 0;
    while(true){
        auto p = st.end(); p--;
        int m = *p;
        st.erase(p);
        auto ptr = st.upper_bound(a+v[idx]-m);
        if(ptr == st.end()) break;
        cnt++;
        st.erase(ptr);
        if(st.empty()){
            break;
        }
    }
    return cnt < M; 
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> N >> M;
    cin >> a;
    for(int i = 0; i < N-1; i++){
        int b; cin >> b;
        v.push_back(b);
    }
    sort(v.begin(), v.end());
    if(N == 2){
        cout << v[0] << endl;
        return 0;
    }
    if(ok(0)){
        cout << v[0] << endl;
        return 0;
    }
    if(!ok(N-2)){
        cout << -1 << endl;
        return 0;
    }
    int l = 0, r = N-2;
    while(r-l > 1){
        int c = (l+r)/2;
        if(ok(c)) r = c;
        else l = c;
    }
    cout << v[r] << endl;
}
0