結果

問題 No.2501 Maximum Inversion Number
ユーザー i_am_noobi_am_noob
提出日時 2023-11-04 23:14:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 1,014 bytes
コンパイル時間 2,530 ms
コンパイル使用メモリ 202,640 KB
実行使用メモリ 5,152 KB
最終ジャッジ日時 2023-11-04 23:14:32
合計ジャッジ時間 4,920 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 46 ms
4,348 KB
testcase_02 AC 76 ms
4,348 KB
testcase_03 AC 55 ms
4,348 KB
testcase_04 AC 51 ms
5,152 KB
testcase_05 AC 49 ms
5,152 KB
testcase_06 AC 56 ms
5,152 KB
testcase_07 AC 47 ms
4,348 KB
testcase_08 AC 47 ms
4,348 KB
testcase_09 AC 28 ms
4,380 KB
testcase_10 AC 48 ms
4,348 KB
testcase_11 AC 47 ms
4,348 KB
testcase_12 AC 46 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 59 ms
5,152 KB
testcase_15 AC 45 ms
4,348 KB
testcase_16 AC 125 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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();
}
0