結果

問題 No.2501 Maximum Inversion Number
ユーザー 沙耶花沙耶花
提出日時 2023-10-13 21:25:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 561 ms / 2,000 ms
コード長 1,068 bytes
コンパイル時間 6,490 ms
コンパイル使用メモリ 258,816 KB
実行使用メモリ 7,892 KB
最終ジャッジ日時 2023-10-13 21:25:18
合計ジャッジ時間 9,188 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 82 ms
4,352 KB
testcase_02 AC 183 ms
4,352 KB
testcase_03 AC 108 ms
4,352 KB
testcase_04 AC 93 ms
7,824 KB
testcase_05 AC 95 ms
7,892 KB
testcase_06 AC 99 ms
7,824 KB
testcase_07 AC 101 ms
4,636 KB
testcase_08 AC 102 ms
4,636 KB
testcase_09 AC 83 ms
4,708 KB
testcase_10 AC 99 ms
4,348 KB
testcase_11 AC 75 ms
4,496 KB
testcase_12 AC 77 ms
4,560 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 124 ms
7,864 KB
testcase_15 AC 79 ms
4,352 KB
testcase_16 AC 561 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 1000000000000000001
long long get(long long n){
	return n*(n-1)/2;
}
int main(){
	
	int _t;
	cin>>_t;
	rep(_,_t){
		
		long long n,m;
		cin>>n>>m;
		vector<long long> l(n),r(n);
		long long L=0,R=0;
		rep(i,n){
			cin>>l[i];
			L += l[i];
		}
		rep(i,n){
			cin>>r[i];
			R += r[i];
		}
		if(m<L || m>R){
			cout<<-1<<endl;
			continue;
		}
		long long ok = 0,ng = 1000000001;
		while(ng-ok>1LL){
			long long mid = (ok+ng)/2;
			long long s = 0;
			rep(i,n){
				s += max(l[i],min(mid,r[i]));
			}
			if(s<m)ok = mid;
			else ng = mid;
		}
		long long s = 0;
		vector<long long> cs(n);
		rep(i,n){
			cs[i] = max(l[i],min(ok,r[i]));
			s += cs[i];
		}
		rep(i,n){
			if(cs[i]==ok && cs[i]<r[i] && s!=m){
				s++;
				cs[i]++;
			}
		}
		long long ans = 0;
		ans = get(m);
		rep(i,n)ans -= get(cs[i]);
		cout<<ans<<endl;
	}
	
	return 0;
}
0