QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#571592 | #9313. Make Max | rainbows | WA | 199ms | 24724kb | C++17 | 1.8kb | 2024-09-18 00:57:04 | 2024-09-18 00:57:04 |
Judging History
answer
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t > 0) {
int n;
cin >> n;
vector<long> arr(n);
unordered_map<long, int> first, last;
for (int i = 0; i < n; i++) {
cin >> arr[i];
last[arr[i]] = i;
}
for (int i = n - 1; i >= 0; i--) {
first[arr[i]] = i;
}
stack<int> stack;
vector<int> left(n, -1);
vector<int> right(n, n);
// Find left limits
for (int i = 0; i < n; i++) {
if (!stack.empty() && arr[i] < arr[stack.top()]) {
left[i] = stack.top();
} else {
stack.push(i);
}
}
// Clear stack for right limits
while (!stack.empty()) {
stack.pop();
}
// Find right limits
for (int i = 0; i < n; i++) {
while (!stack.empty() && arr[i] > arr[stack.top()]) {
right[stack.top()] = i;
stack.pop();
}
stack.push(i);
}
long long ans = 0;
for (const auto &entry: first) {
int index = entry.second;
if (left[index] != -1) {
ans += index - left[index] - 1;
} else {
ans += index;
}
}
for (const auto &entry: last) {
int index = entry.second;
if (right[index] != n) {
ans += right[index] - index - 1;
} else {
ans += n - 1 - index;
}
}
cout << ans << endl;
t--;
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3820kb
input:
4 2 1 2 2 2 2 7 1 1 1 2 2 2 2 3 1 2 3
output:
1 0 3 3
result:
ok 4 number(s): "1 0 3 3"
Test #2:
score: -100
Wrong Answer
time: 199ms
memory: 24724kb
input:
2 198018 875421126 585870339 471894633 383529988 625397685 944061047 704695631 105113224 459022561 760848605 980735314 847376362 980571959 329939331 644635272 326439858 752879510 837384394 175179068 182094523 397239381 1199016 185143405 279638454 252374970 822030887 860312140 137248166 993229443 164...
output:
8064432818 12294547488
result:
wrong answer 1st numbers differ - expected: '4084978', found: '8064432818'