結果
問題 | No.2501 Maximum Inversion Number |
ユーザー |
|
提出日時 | 2023-11-04 23:06:45 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,004 bytes |
コンパイル時間 | 2,064 ms |
コンパイル使用メモリ | 201,092 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-09-25 22:20:57 |
合計ジャッジ時間 | 4,880 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 5 WA * 12 |
ソースコード
#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(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(); }