結果

問題 No.2501 Maximum Inversion Number
ユーザー cho435cho435
提出日時 2023-10-13 23:58:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 602 ms / 2,000 ms
コード長 957 bytes
コンパイル時間 5,027 ms
コンパイル使用メモリ 263,784 KB
実行使用メモリ 8,652 KB
最終ジャッジ日時 2023-10-13 23:58:22
合計ジャッジ時間 8,559 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 90 ms
4,352 KB
testcase_02 AC 195 ms
4,348 KB
testcase_03 AC 124 ms
4,348 KB
testcase_04 AC 110 ms
8,652 KB
testcase_05 AC 116 ms
8,620 KB
testcase_06 AC 144 ms
8,644 KB
testcase_07 AC 115 ms
4,916 KB
testcase_08 AC 120 ms
4,836 KB
testcase_09 AC 85 ms
4,684 KB
testcase_10 AC 111 ms
4,352 KB
testcase_11 AC 86 ms
4,740 KB
testcase_12 AC 88 ms
4,780 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 132 ms
8,564 KB
testcase_15 AC 87 ms
4,352 KB
testcase_16 AC 602 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(int)(n);i++)

void solve(){
	int n,m;
	cin>>n>>m;
	vector<ll> l(n),r(n);
	rep(i,n) cin>>l.at(i);
	rep(i,n) cin>>r.at(i);
	ll sml=0,smr=0;
	rep(i,n){
		sml+=l.at(i);
		smr+=r.at(i);
	}
	if(m<sml||smr<m){
		cout<<"-1\n";
		return;
	}
	ll up=m;
	ll dw=0;
	while(up-dw>1){
		ll md=(up+dw)/2;
		ll tmp=0;
		rep(i,n) tmp+=clamp(md,l.at(i),r.at(i));
		if(tmp>m) up=md;
		else dw=md;
	}
	vector<ll> v(n);
	rep(i,n){
		v.at(i)=clamp(dw,l.at(i),r.at(i));
		m-=v.at(i);
	}
	vector<int> p(n);
	rep(i,n) p.at(i)=i;
	sort(p.begin(),p.end(),[&](int i,int j){
		return v.at(i) < v.at(j);
	});
	for(int i:p){
		if(m<=0) break;
		if(v.at(i)+1<=r.at(i)){
			v.at(i)++;
			m--;
		}
	}
	reverse(v.begin(),v.end());
	ll sm=0;
	ll ans=0;
	rep(i,n){
		ans+=sm*v.at(i);
		sm+=v.at(i);
	}
	cout<<ans<<"\n";
}

int main(){
	int t;
	cin>>t;
	rep(i,t) solve();
}
0