QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#646004#7904. Rainbow SubarrayqiaochenWA 129ms6288kbC++141.7kb2024-10-16 20:51:352024-10-16 20:51:36

Judging History

你现在查看的是最新测评结果

  • [2024-10-16 20:51:36]
  • 评测
  • 测评结果:WA
  • 用时:129ms
  • 内存:6288kb
  • [2024-10-16 20:51:35]
  • 提交

answer

// Problem: K. Rainbow Subarray
// Contest: Codeforces - The 2023 ICPC Asia Jinan Regional Contest (The 2nd Universal Cup. Stage 17: Jinan)
// URL: https://codeforces.com/gym/104901/problem/K
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;

typedef long long i64;

void solve() {
	int n;
	i64 k;
	cin >> n >> k;
	
	vector<i64> a(n + 1);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		a[i] -= i;
	}
	
	multiset<i64> s1, s2;
	i64 sum1 = 0, sum2 = 0;
	
	auto balance = [&]() {
		if (s2.size() > s1.size() + 1) {
			int x = *s2.begin();
			s1.insert(x);
			s2.erase(s2.begin());
			sum1 += x;
			sum2 -= x;
		} else if (s1.size() > s2.size()) {
			int x = *s1.rbegin();
			s2.insert(x);
			s1.erase(prev(s1.end()));
			sum2 += x;
			sum1 -= x;
		}
	};
	
	auto add = [&](int x) {
		if (s2.empty()) {
			s2.insert(x);
			sum2 += x;
		} else {
			if (x >= *s2.begin()) {
				s2.insert(x);
				sum2 += x;
			} else {
				s1.insert(x);
				sum1 += x;
			}
		}
		balance();
	};
	
	auto pop = [&](int x) {
		if (s1.find(x) != s1.end()) {
			s1.erase(x);
			sum1 -= x;
		} else {
			s2.erase(x);
			sum2 -= x;
		}
		balance();
	};
	
	auto check = [&]() -> bool {
		i64 mid = *s2.begin();
		i64 res = sum2 - mid * s2.size() + mid * s1.size() - sum1;
		return res <= k;
	};
	
	int l = 1, ans = 1;
	for (int r = 1; r <= n; r++) {
		add(a[r]);
		while (l <= r && !check()) {
			pop(a[l]);
			l++;
		}
		
		ans = max(ans, r - l + 1);
	}
	
	cout << ans << "\n";
}

signed main() {
	cin.tie(nullptr)->ios::sync_with_stdio(false);
	
	int T;
	cin >> T;
	
	while (T--) {
		solve();
	}

	return 0;	
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3600kb

input:

5
7 5
7 2 5 5 4 11 7
6 0
100 3 4 5 99 100
5 6
1 1 1 1 1
5 50
100 200 300 400 500
1 100
3

output:

4
3
5
1
1

result:

ok 5 lines

Test #2:

score: -100
Wrong Answer
time: 129ms
memory: 6288kb

input:

11102
2 167959139
336470888 134074578
5 642802746
273386884 79721198 396628655 3722503 471207868
6 202647942
268792718 46761498 443917727 16843338 125908043 191952768
2 717268783
150414369 193319712
6 519096230
356168102 262263554 174936674 407246545 274667941 279198849
9 527268921
421436316 3613460...

output:

1
4
3
2
6
5
7
2
4
1
4
1
1
3
2
2
7
8
7
7
1
7
6
2
4
3
1
6
7
7
3
4
3
9
3
8
6
6
3
1
6
3
1
2
4
6
4
6
4
1
4
7
1
6
3
5
6
6
1
7
5
3
1
6
4
5
3
2
2
6
2
3
10
1
4
3
2
4
5
1
7
5
5
5
8
5
3
6
3
5
5
8
5
4
5
2
1
5
2
3
3
4
8
1
3
1
2
2
8
3
1
6
8
1
8
4
5
6
6
8
4
8
3
2
8
4
5
6
2
6
2
4
1
5
4
5
3
2
4
1
2
1
4
5
8
3
7
3
3
3...

result:

wrong answer 11002nd lines differ - expected: '517', found: '515'