QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#763801 | #9584. 顾影自怜 | Fr1nGeLove | WA | 41ms | 46276kb | C++23 | 1.6kb | 2024-11-19 22:08:11 | 2024-11-19 22:08:12 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n, k;
std::cin >> n >> k;
std::vector<int> a(n);
std::vector<std::vector<int>> pos(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
a[i]--;
pos[a[i]].push_back(i);
}
std::vector<int> l(n, -1);
std::vector<int> r(n, n);
std::vector<int> stk;
for (int i = 0; i < n; i++) {
while (!stk.empty() && a[stk.back()] <= a[i]) {
stk.pop_back();
}
if (!stk.empty() && a[stk.back()] > a[i]) {
l[i] = stk.back();
}
stk.push_back(i);
}
stk.clear();
for (int i = n - 1; i >= 0; i--) {
while (!stk.empty() && a[stk.back()] <= a[i]) {
stk.pop_back();
}
if (!stk.empty() && a[stk.back()] > a[i]) {
r[i] = stk.back();
}
stk.push_back(i);
}
std::vector<int> nxt(n, n);
for (int i = 0; i < pos.size(); i++) {
for (int j = 0; j + k - 1 < pos[i].size(); j++) {
nxt[pos[i][j]] = pos[i][j + k - 1];
}
}
i64 ans = 0;
for (int i = 0; i < n; i++) {
int lo = std::max(0, i - l[i]);
int hi = std::max(0, r[i] - nxt[i]);
// std::cerr << i << " " << l[i] << " " << r[i] << " " << nxt[i] << " " << lo * hi << "\n";
ans += 1LL * lo * hi;
}
std::cout << ans << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::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: 3516kb
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: 41ms
memory: 46276kb
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:
166667166667000000
result:
wrong answer 1st lines differ - expected: '500000500000', found: '166667166667000000'