結果

問題 No.2501 Maximum Inversion Number
ユーザー GOTKAKOGOTKAKO
提出日時 2023-10-13 23:48:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 379 ms / 2,000 ms
コード長 1,907 bytes
コンパイル時間 2,577 ms
コンパイル使用メモリ 223,588 KB
実行使用メモリ 25,500 KB
最終ジャッジ日時 2023-10-13 23:48:27
合計ジャッジ時間 6,315 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 95 ms
4,348 KB
testcase_02 AC 120 ms
4,352 KB
testcase_03 AC 103 ms
6,016 KB
testcase_04 AC 213 ms
21,304 KB
testcase_05 AC 270 ms
21,028 KB
testcase_06 AC 203 ms
21,224 KB
testcase_07 AC 64 ms
9,072 KB
testcase_08 AC 165 ms
11,132 KB
testcase_09 AC 31 ms
4,556 KB
testcase_10 AC 92 ms
5,796 KB
testcase_11 AC 94 ms
9,992 KB
testcase_12 AC 124 ms
10,344 KB
testcase_13 AC 3 ms
4,484 KB
testcase_14 AC 253 ms
25,500 KB
testcase_15 AC 111 ms
6,400 KB
testcase_16 AC 379 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;


int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T; cin >> T;
    while(T--){
        long long N,M; cin >> N >> M;
        vector<pair<long long,long long>> RL(N);
        for(int i=0; i<2; i++){
            for(int k=0; k<N; k++){
                if(i) cin >> RL.at(k).first;
                else cin >> RL.at(k).second;
            }
        }
        sort(RL.begin(),RL.end());

        long long minlen = 0,maxlen = 0;
        for(int i=0; i<N; i++){
            minlen += RL.at(i).second;
            maxlen += RL.at(i).first;}
        if(minlen > M || maxlen < M){cout << -1 << endl; continue;}
        M -= minlen;

        priority_queue<tuple<long long,long long,bool>,vector<tuple<long long,long long,bool>>,greater<tuple<long long,long long,bool>>> Q;
        for(int i=0; i<N; i++){
            auto[r,l] = RL.at(i);
            Q.push({l,i,false}); Q.push({r,i,true}); 
        }
    
        long long back = 0;
        set<int> st;
        vector<long long> use(N);
        for(int i=0; i<N; i++) tie(ignore,use.at(i)) = RL.at(i);

        while(Q.size()){
            auto[a,pos,start] = Q.top(); Q.pop();
            long long add = a-back,ope = add*st.size();
            if(M < ope){
                add = M/st.size(); ope = add*st.size();
                back += add; M -= ope;
                for(auto x : st){
                    use.at(x) = back;
                    if(M){use.at(x)++; M--;} 
                }
                break;
            }
            back += add; M -= ope;
            if(start == false) st.insert(pos);
            else{st.erase(pos); use.at(pos) = back;} 
        }

        long long answer = 0,sum = 0;
        for(int i=0; i<N; i++){
            long long u = use.at(i);
            answer += u*sum;
            sum += u;
        }
        cout << answer << endl;
    }

}
0