結果

問題 No.2501 Maximum Inversion Number
ユーザー HaaHaa
提出日時 2023-10-13 22:12:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 578 ms / 2,000 ms
コード長 1,534 bytes
コンパイル時間 1,616 ms
コンパイル使用メモリ 170,468 KB
実行使用メモリ 8,372 KB
最終ジャッジ日時 2023-10-13 22:13:03
合計ジャッジ時間 4,788 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 80 ms
4,348 KB
testcase_02 AC 186 ms
4,348 KB
testcase_03 AC 111 ms
4,348 KB
testcase_04 AC 95 ms
8,368 KB
testcase_05 AC 92 ms
8,300 KB
testcase_06 AC 106 ms
8,164 KB
testcase_07 AC 98 ms
4,796 KB
testcase_08 AC 98 ms
4,876 KB
testcase_09 AC 81 ms
4,684 KB
testcase_10 AC 96 ms
4,348 KB
testcase_11 AC 77 ms
5,088 KB
testcase_12 AC 80 ms
5,012 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 123 ms
8,372 KB
testcase_15 AC 77 ms
4,348 KB
testcase_16 AC 578 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
template<typename T> bool chmax(T& x, const T& y){return (x<y)?(x=y,true):false;};
template<typename T> bool chmin(T& x, const T& y){return (x>y)?(x=y,true):false;};
constexpr ll MOD=998244353;
constexpr ll INF=2e18;

void solve(){
    ll n, m; cin >> n >> m;
    VI l(n), r(n);
    REP(i,n) cin >> l[i];
    REP(i,n) cin >> r[i];
    ll lsum=0, rsum=0;
    REP(i,n) lsum+=l[i];
    REP(i,n) rsum+=r[i];
    if(m<lsum||rsum<m){
        cout << -1 << endl;
        return;
    }
    ll x=0, y=2e9;
    ll rem=m-lsum;
    while(y-x>1){
        ll z=(x+y)/2;
        ll sum=0;
        REP(i,n){
            if(z<l[i])
                sum+=l[i];
            else if(r[i]<z)
                sum+=r[i];
            else
                sum+=z;
        }
        if(sum<=m){
            x=z;
            chmin(rem,m-sum);
        }
        else
            y=z;
    }
    VI a;
    REP(i,n){
        if(x<l[i])
            a.push_back(l[i]);
        else if(r[i]<=x)
            a.push_back(r[i]);
        else{
            if(rem-->0)
                a.push_back(x+1);
            else
                a.push_back(x);
        }
    }
    ll ans=0, sum=0;
    REP(i,n){
        ans+=sum*a[n-1-i];
        sum+=a[n-1-i];
    }
    cout << ans << endl;
}

int main(){
    int t; cin >> t;
    while(t--) solve();
    return 0;
}
0