結果

問題 No.2501 Maximum Inversion Number
ユーザー GOTKAKOGOTKAKO
提出日時 2023-10-13 23:34:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,898 bytes
コンパイル時間 2,118 ms
コンパイル使用メモリ 189,696 KB
実行使用メモリ 26,816 KB
最終ジャッジ日時 2023-10-13 23:34:28
合計ジャッジ時間 5,968 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 WA -
testcase_02 AC 119 ms
4,352 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 69 ms
9,120 KB
testcase_08 WA -
testcase_09 AC 32 ms
4,688 KB
testcase_10 AC 92 ms
5,656 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 250 ms
26,816 KB
testcase_15 WA -
testcase_16 AC 386 ms
4,352 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:30:17: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   30 |             auto[r,l] = RL.at(i);
      |                 ^
main.cpp:40:17: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   40 |             auto[a,pos,start] = Q.top(); Q.pop();
      |                 ^

ソースコード

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,true}); Q.push({r,i,false}); 
        }
    
        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) 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