結果

問題 No.2501 Maximum Inversion Number
ユーザー Алексей ДанилюкАлексей Данилюк
提出日時 2023-10-13 21:32:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 127 ms / 2,000 ms
コード長 2,746 bytes
コンパイル時間 1,285 ms
コンパイル使用メモリ 125,352 KB
実行使用メモリ 8,244 KB
最終ジャッジ日時 2023-10-13 21:32:49
合計ジャッジ時間 3,592 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,564 KB
testcase_01 AC 43 ms
5,636 KB
testcase_02 AC 71 ms
5,524 KB
testcase_03 AC 55 ms
5,944 KB
testcase_04 AC 56 ms
8,244 KB
testcase_05 AC 48 ms
8,128 KB
testcase_06 AC 59 ms
8,232 KB
testcase_07 AC 47 ms
6,520 KB
testcase_08 AC 46 ms
6,484 KB
testcase_09 AC 34 ms
6,796 KB
testcase_10 AC 43 ms
5,816 KB
testcase_11 AC 40 ms
6,576 KB
testcase_12 AC 41 ms
6,452 KB
testcase_13 AC 2 ms
5,560 KB
testcase_14 AC 53 ms
8,236 KB
testcase_15 AC 40 ms
5,796 KB
testcase_16 AC 127 ms
5,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,sse4a,avx,avx2,popcnt,tune=native")
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
#include <climits>
using namespace std;

#ifdef LOCAL
	#define eprintf(...) {fprintf(stderr, __VA_ARGS__);fflush(stderr);}
#else
	#define eprintf(...) 42
#endif

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
	return (ull)rng() % B;
}

#define mp make_pair
#define all(x) (x).begin(),(x).end()

clock_t startTime;
double getCurrentTime() {
	return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}

ll floor_div(ll x, ll y) {
	assert(y != 0);
	if (y < 0) {
		y = -y;
		x = -x;
	}
	if (x >= 0) return x / y;
	return (x + 1) / y - 1;
}
ll ceil_div(ll x, ll y) {
	assert(y != 0);
	if (y < 0) {
		y = -y;
		x = -x;
	}
	if (x <= 0) return x / y;
	return (x - 1) / y + 1;
}

const int N = 200200;
ll l[N], r[N];
ll a[N];
int n;
ll m;

ll setTo(ll x) {
	ll sum = 0;
	for (int i = 0; i < n; i++) {
		if (r[i] < x) {
			a[i] = r[i];
		} else if (l[i] > x) {
			a[i] = l[i];
		} else {
			a[i] = x;
		}
		sum += a[i];
	}
	return sum;
}

void solve() {
	scanf("%d%lld", &n, &m);
	for (int i = 0; i < n; i++)
		scanf("%lld", &l[i]);
	for (int i = 0; i < n; i++)
		scanf("%lld", &r[i]);
	ll L = -1, R = m + 1;
	while(R - L > 1) {
		ll x = (L + R) / 2;
		if (setTo(x) < m)
			L = x;
		else
			R = x;
	}
	ll sum = setTo(R) - m;
	for (int i = 0; i < n; i++) {
		if (sum > 0 && a[i] == R && l[i] < R) {
			a[i]--;
			sum--;
		}
	}
	if (sum != 0) {
		printf("-1\n");
		return;
	}
	ll ans = m * m;
	for (int i = 0; i < n; i++)
		ans -= a[i] * a[i];
	ans /= 2;
	printf("%lld\n", ans);
}

int main() {
	startTime = clock();
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	int t;
	scanf("%d", &t);
	for (int i = 1; i <= t; i++) {
		eprintf("--- Case #%d start ---\n", i);
		//printf("Case #%d: ", i);

		solve();

		//printf("%lld\n", (ll)solve());

		/*
		if (solve()) {
			printf("Yes\n");
		} else {
			printf("No\n");
		}
		*/

		eprintf("--- Case #%d end ---\n", i);
		eprintf("time = %.5lf\n", getCurrentTime());
		eprintf("++++++++++++++++++++\n");
	}



	return 0;
}
0