QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#716677#9584. 顾影自怜PHarrWA 67ms85300kbC++201.4kb2024-11-06 15:47:372024-11-06 15:47:37

Judging History

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

  • [2024-11-06 15:47:37]
  • 评测
  • 测评结果:WA
  • 用时:67ms
  • 内存:85300kb
  • [2024-11-06 15:47:37]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;


using i64 = long long;
using ui32 = unsigned int;

using vi = vector<int>;

void solve() {
	int n, k;
	cin >> n >> k;

	vi a(n + 1);
	for(int i = 1; i <= n; i ++)
		cin >> a[i];

	vector vis(n + 1, vi(1, 0));

	for(int i = 1;i <= n; i ++)
		vis[a[i]].push_back(i);

	for(int i = 1; i <= n; i ++)
		vis[i].push_back(n + 1);

	vi lst(n + 1), nxt(n + 1, -1), ed(n + 1, -1);
	for(int i = 1; i <= n; i ++) {
		if(vis[i].size() - 2 < k) continue;
		for(int l = 1, r = k; r + 1 < vis[i].size(); l ++, r ++) {
			lst[vis[i][l]] = vis[i][l - 1];
			ed[vis[i][l]] = vis[i][r];
			nxt[vis[i][l]] = vis[i][r + 1];
		}
	}

	vi L(n + 1);
	stack<int> stk;
	for(int i = 1; i <= n; i ++) {
		while(not stk.empty() and a[stk.top()] <= a[i])
			stk.pop();
		if(stk.empty()) L[i] = 0;
		else L[i] = stk.top();
		stk.push(i); 
	}

	vi R(n + 1);
	stk = stack<int>();
	for(int i = n; i >= 1; i --) {
		while(not stk.empty() and a[stk.top()] <= a[i])
			stk.pop();
		if(stk.empty()) R[i] = n + 1;
		else R[i] = stk.top();
		stk.push(i);
	}
	int res = 0;
	for(int i = 1; i <= n; i ++){
		if(nxt[i] == -1) continue;
		int l = i - max(lst[i], L[i]);
		int r = min(nxt[i], R[i]) - ed[i];
		res += l * r;
	}
	cout << res << "\n";
	return ;
}

int main() {
	ios::sync_with_stdio(false), cin.tie(nullptr);

	int T;
	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: 3656kb

input:

2
5 2
1 3 3 2 2
4 3
1 4 2 1

output:

7
0

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 67ms
memory: 85300kb

input:

1
1000000 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

output:

1000000

result:

wrong answer 1st lines differ - expected: '500000500000', found: '1000000'