UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#202213#3533. 数组shiruiheng100962ms5908kbC++111.3kb2024-02-14 10:08:012024-02-14 12:33:10

answer

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
//using LL = __int128
int n, a[100010], cnt[12][100010], maxn; 
int pos[2][100010], t[2];
int calc(int k, int l, int r){
	return cnt[k][r] - cnt[k][l - 1];
}
ll solve(int l, int r){
	if(l == r)
		return 1;
	int mid = (l + r) / 2;
	ll ans = solve(l, mid) + solve(mid + 1, r);
	for(int col = 1 ; col <= maxn ; col++){
		t[0] = t[1] = 0;
		for(int i = l ; i <= mid ; i++)
			pos[0][++t[0]] = 2 * calc(col, i, mid) + i - 2;
		for(int i = mid + 1 ; i <= r ; i++)
			pos[1][++t[1]] = i - 2 * calc(col, mid + 1, i);
		sort(pos[0] + 1, pos[0] + 1 + t[0]);
		sort(pos[1] + 1, pos[1] + 1 + t[1]);
		for(int i = 1 ; i <= t[0] ; i++){
			int pos1 = upper_bound(pos[1] + 1, pos[1] + 1 + t[1], pos[0][i]) - pos[1];
			ans += pos1 - 1;
		}
	}
	return ans;
}
int main(){
	scanf("%lld", &n);
	for(int i = 1 ; i <= n ; i++){
		scanf("%lld", a + i);
		maxn = max(maxn, a[i]);
		for(int j = 1 ; j <= 10 ; j++)
			cnt[j][i] = cnt[j][i - 1];
		cnt[a[i]][i]++;
	}
	if(n <= 1000){
		ll ans = n;
		for(int i = 1 ; i <= n ; i++){
			for(int j = i + 1 ; j <= n ; j++)
				for(int k = 1 ; k <= 10 ; k++)
					if(cnt[k][j] - cnt[k][i - 1] > ((j - i + 1) >> 1)){
						ans++;
						break;
					}
		}
		printf("%lld", ans);
		exit(0);
	}
	printf("%lld", solve(1, n));
	return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 0ms
memory: 1248kb

input:

100
1 1 1 1 1 8 1 1 8 1 4 2 2 2 2 2 2 2 2 8 3 3 5 3 3 6 3 3 3 3 4 4 4 4 2 4 4 4 4 4 5 5 2 9 5 5 7 9 ...

output:

844

result:

ok single line: '844'

Test #2:

score: 10
Accepted
time: 5ms
memory: 1292kb

input:

1000
1 1 1 8 1 1 1 1 5 1 1 9 1 4 8 8 1 1 1 1 1 1 1 10 1 1 1 1 9 1 1 1 1 1 1 1 1 10 1 1 1 1 1 1 2 1 1...

output:

95463

result:

ok single line: '95463'

Test #3:

score: 10
Accepted
time: 4ms
memory: 1292kb

input:

1000
1 1 1 10 5 8 1 1 1 1 6 7 1 1 1 1 1 1 1 1 1 6 9 1 1 1 9 1 1 1 5 9 9 3 1 4 3 1 1 1 1 3 10 1 4 7 4...

output:

88434

result:

ok single line: '88434'

Test #4:

score: 10
Accepted
time: 19ms
memory: 1724kb

input:

10000
1 3 1 1 1 1 1 6 1 1 1 1 7 5 1 1 1 1 1 1 3 3 3 1 1 1 1 7 1 1 4 1 1 8 1 9 1 1 1 7 1 1 1 1 1 2 1 ...

output:

9306221

result:

ok single line: '9306221'

Test #5:

score: 10
Accepted
time: 15ms
memory: 1720kb

input:

10000
1 1 1 1 9 1 10 1 1 1 1 1 1 1 3 1 1 1 1 8 1 1 1 10 1 1 1 1 1 1 5 1 6 1 8 1 3 9 5 1 5 3 1 1 3 1 ...

output:

9463489

result:

ok single line: '9463489'

Test #6:

score: 10
Accepted
time: 98ms
memory: 5908kb

input:

100000
2 2 2 2 1 1 2 2 2 1 2 1 1 2 2 2 2 1 1 1 1 1 1 2 2 2 2 2 1 2 1 2 2 1 2 1 2 2 1 1 1 2 2 2 2 2 1...

output:

4980632044

result:

ok single line: '4980632044'

Test #7:

score: 10
Accepted
time: 99ms
memory: 5904kb

input:

100000
1 2 1 1 1 2 1 1 1 2 1 1 2 1 2 2 1 1 2 2 1 2 1 2 2 2 2 2 1 1 1 1 1 1 1 1 2 1 2 2 1 2 1 1 1 2 1...

output:

4980856291

result:

ok single line: '4980856291'

Test #8:

score: 10
Accepted
time: 232ms
memory: 5904kb

input:

100000
1 1 1 2 1 1 1 1 1 1 1 2 2 1 1 2 1 1 1 1 1 1 1 1 2 2 1 2 1 1 2 2 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1...

output:

992469861

result:

ok single line: '992469861'

Test #9:

score: 10
Accepted
time: 245ms
memory: 5904kb

input:

100000
1 1 2 1 5 3 1 1 1 2 1 2 10 10 1 1 1 1 1 1 3 1 1 1 3 1 1 8 1 1 1 1 1 1 4 1 1 1 1 5 1 1 1 1 6 8...

output:

963930312

result:

ok single line: '963930312'

Test #10:

score: 10
Accepted
time: 245ms
memory: 5904kb

input:

100000
6 1 1 6 1 1 1 1 2 1 5 4 1 6 1 10 1 1 1 1 1 2 7 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 1 2 1 8 1 ...

output:

963908847

result:

ok single line: '963908847'

Extra Test:

score: 0
Extra Test Passed