QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#576506#9313. Make MaxXwcWA 33ms8544kbC++141.5kb2024-09-19 20:43:092024-09-19 20:43:12

Judging History

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

  • [2024-09-19 20:43:12]
  • 评测
  • 测评结果:WA
  • 用时:33ms
  • 内存:8544kb
  • [2024-09-19 20:43:09]
  • 提交

answer

#include <bits/stdc++.h>

#define fastio ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)

typedef double ff;
typedef long long int ll;
typedef unsigned long long int ull;
using namespace std;

typedef pair<ll, ll> pii;

const int N = 2e5 + 10, M = 1e7, mod = 998244353;

ll n, a[N], pos[N];
ll f[N];

ll dfs(int u)
{
	if(f[u] != -1) return f[u];
	else {
		f[u] = dfs(pos[u]) + 1;
		return f[u];
	}
}

void solve()
{
	cin >> n;
	
	for(int i = 1; i <= n; i ++ ) cin >> a[i], f[i] = pos[i] = -1;
	
	stack<int> stk;
	for(int i = 1; i <= n; i ++ ) {
		while(stk.size() && a[i] >= a[stk.top()]) stk.pop();
		if(stk.size()) pos[i] = stk.top();
		stk.push(i);
	}
	
	while(stk.size()) stk.pop();
	for(int i = n; i >= 1; i -- ) {
		while(stk.size() && a[i] >= a[stk.top()]) stk.pop();
		if(stk.size()) {
			int head = stk.top();
			if(pos[i] == -1) pos[i] = head;
			else {
				if(i - pos[i] == head - i) {
					if(a[head] < a[pos[i]]) pos[i] = head;
				} else {
					if(head - i < i - pos[i]) pos[i] = head;
 				}
			}
		}
		stk.push(i);
	}
	
	// for(int i = 1; i <= n; i ++ ) {
		// cout << pos[i] << ' ';
	// }
	// cout << '\n';
	for(int i = 1; i <= n; i ++ ) {
		if(pos[i] == -1) f[i] = 0;
	}
	
	for(int i = 1; i <= n; i ++ ) {
		if(f[i] != -1) continue;
		f[i] = dfs(pos[i]) + 1;
	}
	
	ll ans = 0;
	for(int i = 1; i <= n; i ++ ) {
		ans += f[i];
		// cout << f[i] << ' ';
	}
	cout << ans << '\n';
}

int main()
{
	fastio;
	
	int t = 1;
	cin >> t;

	while( t -- ) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 7712kb

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: 33ms
memory: 8544kb

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:

2235680
2369724

result:

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