QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#567901 | #9313. Make Max | psycho_009 | WA | 106ms | 10268kb | C++14 | 1.7kb | 2024-09-16 14:33:20 | 2024-09-16 14:33:21 |
Judging History
answer
#include<iostream>
#include<algorithm>
#include <cstring>
#include <queue>
using namespace std;
const int N = 200010, INF = 0x3f3f3f3f;
int h[N], ne[N], e[N], idx;
int r[N], l[N];
int stack1[N], q[N];
bool st[N];
int dist[N];
int n, maxv;
void add(int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
void spfa() {
queue<int> heap;
memset(st, 0, sizeof st);
memset(dist, 0x3f, sizeof dist);
for (int i = 1; i <= n; i ++ )
if (q[i] == maxv) {
heap.push(i);
dist[i] = 0;
}
while(heap.size()) {
auto t = heap.front();
heap.pop();
st[t] = false;
for (int i = h[t]; i != -1; i = ne[i]) {
int j = e[i];
if (dist[j] > dist[t] + 1) {
dist[j] = dist[t] + 1;
if (!st[j]) {
st[j] = true;
heap.push(j);
}
}
}
}
}
void solve() {
memset(h, -1, sizeof h);
cin >> n;
maxv = 0;
q[0] = INF, q[n + 1] = INF;
for (int i = 1; i <= n; i ++ ) {
cin >> q[i];
maxv = max(maxv, q[i]);
}
int hh = -1;
// 左边第一个的元素
for (int i = 1; i <= n; i ++ ) {
while(hh >= 0 && q[stack1[hh]] <= q[i]) hh --;
r[i] = stack1[hh];
stack1[++ hh] = i;
}
hh = -1;
// 右边第一个比它大
for (int i = n; i > 0; i -- ) {
while (hh >= 0 && q[stack1[hh]] <= q[i]) hh --;
l[i] = stack1[hh];
stack1[++ hh] = i;
}
for (int i = 1; i <= n; i ++ ) {
if (q[i] != maxv) {
int id = q[r[i]] < q[l[i]] ? r[i] : l[i];
add(id, i);
}
}
spfa();
long long res = 0;
for (int i = 1; i <= n; i ++ ) res += dist[i];
cout << res << endl;
}
int main() {
int T;
cin >> T;
while (T -- )
solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 7356kb
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: 106ms
memory: 10268kb
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:
4084978 202401344357415
result:
wrong answer 2nd numbers differ - expected: '4130372', found: '202401344357415'