結果
問題 | No.2501 Maximum Inversion Number |
ユーザー |
|
提出日時 | 2023-11-04 23:14:27 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 125 ms / 2,000 ms |
コード長 | 1,014 bytes |
コンパイル時間 | 2,159 ms |
コンパイル使用メモリ | 195,100 KB |
最終ジャッジ日時 | 2025-02-17 19:19:20 |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 17 |
ソースコード
#include<bits/stdc++.h> using namespace std; using ll=long long; using pii=pair<int,int>; #define all(a) a.begin(),a.end() #define pb push_back #define sz(a) ((int)a.size()) const int N=200005; int n,m,a[N],b[N]; void ahcorz(){ cin >> n >> m; for(int i=0; i<n; ++i) cin >> a[i]; for(int i=0; i<n; ++i) cin >> b[i]; if(accumulate(a,a+n,0ll)>m||accumulate(b,b+n,0ll)<m){ cout << "-1\n"; return; } int ok=0,ng=1<<30; while(ok+1<ng){ int mid=ok+ng>>1; ll cnt=0; for(int i=0; i<n; ++i) cnt+=min(b[i],max(a[i],mid)); if(cnt<=m) ok=mid; else ng=mid; } for(int i=0; i<n; ++i) a[i]=min(b[i],max(a[i],ok)); ll lft=m-accumulate(a,a+n,0ll); for(int i=0; i<n; ++i) if(a[i]==ok&&b[i]>a[i]&&lft>0) lft--,a[i]++; ll res=1ll*m*(m-1)/2; for(int i=0; i<n; ++i) res-=1ll*a[i]*(a[i]-1)/2; cout << res << "\n"; } signed main(){ ios_base::sync_with_stdio(0),cin.tie(0); int t; cin >> t; while(t--) ahcorz(); }