QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#567943#9313. Make Maxpsycho_009WA 0ms11836kbC++141.7kb2024-09-16 14:46:032024-09-16 14:46:03

Judging History

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

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

answer

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

using namespace std;
typedef long long LL;
const int N = 200010, M = 400010;
const LL INF = 9223372036854775808;


int h[M], ne[M], e[M], 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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 11836kb

input:

4
2
1 2
2
2 2
7
1 1 1 2 2 2 2
3
1 2 3

output:

1061109567
0
3183328701
2122219134

result:

wrong answer 1st numbers differ - expected: '1', found: '1061109567'