結果
問題 | No.2501 Maximum Inversion Number |
ユーザー |
|
提出日時 | 2023-10-13 23:48:21 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 357 ms / 2,000 ms |
コード長 | 1,907 bytes |
コンパイル時間 | 2,251 ms |
コンパイル使用メモリ | 216,608 KB |
最終ジャッジ日時 | 2025-02-17 07:38:47 |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 17 |
ソースコード
#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; } }