QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#200620#6433. Klee in Solitary ConfinementAshokaWA 0ms3776kbC++201.8kb2023-10-04 18:00:292023-10-04 18:00:29

Judging History

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

  • [2023-10-04 18:00:29]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3776kb
  • [2023-10-04 18:00:29]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

void solve() {
	int n, k;
	cin >> n >> k;
	vector<int> a(n);
	set<int> _set;
	map<int, int> cnt;
	map<int, vector<int>> mp;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
		_set.emplace(a[i]);
		cnt[a[i]]++;
		mp[a[i]].emplace_back(i);
	}
	int ans = 0;
	for (auto x : _set) {
		ans = max(ans, cnt[x]);
	}
	if (k == 0) {
		cout << ans << "\n";
		return ;
	}
	for (auto x : _set) {
		vector<int> &px = mp[x], &py = mp[x + k];
		if (!py.size()) continue;
		int lx = 0, rx = (int)px.size() - 1;
		int ly = upper_bound(py.begin(), py.end(), px[lx]) - py.begin();
		int ry = upper_bound(py.begin(), py.end(), px[rx]) - py.begin() - 1;
		if (ly > ry) {
			ans = max(ans, cnt[x] + cnt[x + k]);
			continue;
		}
		int mx = rx + 1 - (ry - ly + 1);
		int tl = lx, tr = rx;
		while (lx < rx) {
			int tly = upper_bound(py.begin(), py.end(), px[lx + 1]) - py.begin();
			if (tly > ry) {
				if (rx - lx > mx) {
					mx = rx - lx;
					tl = lx + 1;
				}
				break;
			}
			int t = rx - lx - (ry - tly + 1);
			if (t >= mx) {
				mx = t;
				tl = lx + 1;
			}
			++lx;
		}
		lx = tl;
		ly = upper_bound(py.begin(), py.end(), px[lx]) - py.begin();
		while (lx < rx) {
			int tmpry = upper_bound(py.begin(), py.end(), px[rx - 1]) - py.begin() - 1;
			if (ly > tmpry) {
				if (rx - lx > mx) {
					tr = rx - 1;
					mx = rx - lx;
				}
				break;
			}
			int t = rx - lx - (tmpry - ly + 1);
			if (t >= mx) {
				mx = t;
				tr = rx - 1;
			}
			--rx;
		}
		// cerr << x << ":" << mx << "\n";
		ans = max(ans, mx + cnt[x + k]);
	}
	cout << ans << "\n";
	return ;
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	int T = 1;
	// cin >> T;
	while (T--) {
		solve();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5 2
2 2 4 4 4

output:

5

result:

ok 1 number(s): "5"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3556kb

input:

7 1
3 2 3 2 2 2 3

output:

6

result:

ok 1 number(s): "6"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3532kb

input:

7 1
2 3 2 3 2 3 3

output:

5

result:

ok 1 number(s): "5"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3748kb

input:

9 -100
-1 -2 1 2 -1 -2 1 -2 1

output:

3

result:

ok 1 number(s): "3"

Test #5:

score: -100
Wrong Answer
time: 0ms
memory: 3776kb

input:

200 121649
0 527189 -1000000 -306471 -998939 527189 -1000000 -1000000 0 527189 0 527189 0 527189 -306471 -998939 -306471 -306471 -306471 0 0 527189 527189 1000000 527189 -1000000 1000000 648838 -1000000 -998939 -998939 -998939 0 1000000 -1000000 -998939 527189 1000000 648838 -1000000 1000000 648838 ...

output:

33

result:

wrong answer 1st numbers differ - expected: '37', found: '33'