QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#567922#9313. Make Maxpsycho_009WA 109ms11616kbC++141.7kb2024-09-16 14:39:572024-09-16 14:40:02

Judging History

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

  • [2024-09-18 15:56:24]
  • hack成功,自动添加数据
  • (/hack/836)
  • [2024-09-16 14:40:02]
  • 评测
  • 测评结果:WA
  • 用时:109ms
  • 内存:11616kb
  • [2024-09-16 14:39:57]
  • 提交

answer

#include<iostream>
#include<algorithm>
#include <cstring>
#include <queue>

using namespace std;
const int N = 200010, INF = 0x3f3f3f3f;
typedef long long LL;

int h[N], ne[N], e[N], idx;
int r[N], l[N];
LL stack1[N], q[N];
bool st[N];
int dist[N];
LL 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();
	
	LL 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: 7580kb

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: 109ms
memory: 11616kb

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'