結果

問題 No.2501 Maximum Inversion Number
ユーザー kotatsugamekotatsugame
提出日時 2023-10-13 21:36:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 121 ms / 2,000 ms
コード長 1,087 bytes
コンパイル時間 1,638 ms
コンパイル使用メモリ 76,288 KB
実行使用メモリ 6,248 KB
最終ジャッジ日時 2023-10-13 21:37:01
合計ジャッジ時間 3,811 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 48 ms
4,352 KB
testcase_02 AC 67 ms
4,348 KB
testcase_03 AC 57 ms
4,348 KB
testcase_04 AC 64 ms
6,248 KB
testcase_05 AC 64 ms
6,248 KB
testcase_06 AC 68 ms
6,176 KB
testcase_07 AC 52 ms
4,348 KB
testcase_08 AC 54 ms
4,356 KB
testcase_09 AC 25 ms
4,348 KB
testcase_10 AC 49 ms
4,352 KB
testcase_11 AC 56 ms
4,352 KB
testcase_12 AC 56 ms
4,356 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 61 ms
6,228 KB
testcase_15 AC 47 ms
4,348 KB
testcase_16 AC 121 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<cassert>
using namespace std;
int N,M;
int L[2<<17],R[2<<17];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int T;cin>>T;
	for(;T--;)
	{
		cin>>N>>M;
		long sL=0,sR=0;
		for(int i=0;i<N;i++)
		{
			cin>>L[i];
			sL+=L[i];
		}
		for(int i=0;i<N;i++)
		{
			cin>>R[i];
			sR+=R[i];
		}
		if(M<sL||sR<M)
		{
			cout<<"-1\n";
			continue;
		}
		int mnl=-1,mnr=1e9;
		while(mnr-mnl>1)
		{
			int mid=(mnl+mnr)/2;
			long s=0;
			for(int i=0;i<N;i++)
			{
				if(mid<L[i])s+=L[i];
				else if(R[i]<mid)s+=R[i];
				else s+=mid;
			}
			if(s>=M)mnr=mid;
			else mnl=mid;
		}
		vector<pair<int,int> >cnt(N);
		long s=0;
		for(int i=0;i<N;i++)
		{
			int now=mnr<L[i]?L[i]:mnr>R[i]?R[i]:mnr;
			cnt[i]=make_pair(now,i);
			s+=now;
		}
		assert(0<=s-M&&s-M<N);
		sort(cnt.begin(),cnt.end());
		long ans=(long)M*(M-1)/2;
		for(int i=cnt.size();i--;)
		{
			int v=cnt[i].first;
			int id=cnt[i].second;
			if(s>M)
			{
				if(L[id]<v)v--,s--;
			}
			ans-=(long)v*(v-1)/2;
		}
		assert(s==M);
		cout<<ans<<"\n";
	}
}
0