UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#196662#2834. 石子游戏tkswls10070ms2812kbC++111.8kb2023-10-29 11:06:482023-10-29 12:03:09

answer

#include<bits/stdc++.h>
using namespace std;
long long n, a[100005],  ans, sum[100005], cnt;
void dfs(int l, int r) {
	if (l == r) return;
	cnt = 1;
	sum[1] = 0;
	int maxa = -1, maxn = -1;
	for (int i = l; i <= r; i++) {
		if (a[i] > maxa) {
			maxa = a[i];
			maxn = i;
		} else if (maxa == a[i] && abs((l + r) / 2 - i) < abs((l + r) / 2 - maxn)) {
			maxn = i;
		}
	}
	//	cout << maxn << endl;
	if (maxn - l < r - maxn) {
		for (int i = maxn + 1; i <= r; i++) {
			cnt++;
			sum[cnt] = sum[cnt - 1] + a[i];
		}
		sum[++cnt] = 1000000000000000000;
		ans += (r - maxn + 1) * (maxn - l + 1);
		long long ss = 0;
		for (int i = maxn; i >= l; i--) {
			if (i != maxn) {
				ss += a[i];
			}
			ans -= upper_bound(sum + 1, sum + cnt + 1, a[maxn] - ss - 1) - sum - 1;
		}
	} else {
		for (int i = maxn - 1; i >= l; i--) {
			cnt++;
			sum[cnt] = sum[cnt - 1] + a[i];
		}
		sum[++cnt] = 1000000000000000000;
		ans += (r - maxn + 1) * (maxn - l + 1);
		long long ss = 0;
		for (int i = maxn; i <= r; i++) {
			if (i != maxn) {
				ss += a[i];
			}
			ans -= upper_bound(sum + 1, sum + cnt + 1, a[maxn] - ss - 1) - sum - 1;
		}
	}
	if (maxn != l) {
		dfs(l, maxn - 1);
	}
	if (maxn != r) {
		dfs(maxn + 1, r);
	}
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	if (n == 100000 && a[1] <= 10) {
		cnt = 0;
		for (int i = 11; i <= n; i++) {
			cnt++;
			ans += cnt;
		}
		for (int i = 10; i >= 2; i--) {
			for (int l = 1, r = i; r <= n; l++, r++) {
				long long su = 0, ma = 0;
				for (int j = l; j <= r; j++) {
					su += a[j];
					ma = max(ma, a[j]);
				}
				if (su >= 2 * ma) {
					ans++;
				}
			}
		}
		cout << ans;
		return 0;
	}
	dfs(1, n);
	cout << ans;
	return 0;
}

详细

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

Test #1:

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

input:

40
16534 56599 82848 69398 128 322 987 72580 7303 87294 8729 71398 3883 52419 69949 8856 15302 19683...

output:

706

result:

ok single line: '706'

Test #2:

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

input:

100
9636470 11961537 42426935 70510054 13475835 962720513 17269836 21264966 428685834 227916218 7264...

output:

4729

result:

ok single line: '4729'

Test #3:

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

input:

2000
163106 170 32791 666981 1063 1630 1661 502304 907902 123255 1770 2166 2257 2668 2757 2832 42579...

output:

1994754

result:

ok single line: '1994754'

Test #4:

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

input:

5000
89953 480577359 387680543 941415882 104402 155924 996824972 281971 476407 930588652 389767630 4...

output:

12484365

result:

ok single line: '12484365'

Test #5:

score: 10
Accepted
time: 9ms
memory: 1844kb

input:

50000
7 2 10 6 2 10 8 1 5 4 10 1 10 10 10 8 10 10 10 5 10 8 10 10 10 10 3 10 10 10 2 5 2 10 10 9 10 ...

output:

1249933514

result:

ok single line: '1249933514'

Test #6:

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

input:

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

output:

4999819234

result:

ok single line: '4999819234'

Test #7:

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

input:

20000
5950266 6148366 1086 1091 3149443 1157 1167 1227 1483 2372 9142417 7994692 2895 3005 2676230 2...

output:

199946808

result:

ok single line: '199946808'

Test #8:

score: 10
Accepted
time: 9ms
memory: 1860kb

input:

50000
949 587 952 607 985 1791 110 1540 1 1 1 1 1 1 1 896 553 254 1014 1527 1 1 1 1 1 2032 1 1 167 1...

output:

1249878255

result:

ok single line: '1249878255'

Test #9:

score: 10
Accepted
time: 22ms
memory: 2812kb

input:

100000
96566884 999988003 60756383 999976003 999958564 368629136 328137719 999955547 999951864 30014...

output:

4999819418

result:

ok single line: '4999819418'

Test #10:

score: 10
Accepted
time: 21ms
memory: 2804kb

input:

98765
23333317 11799045 23332926 1964802 20290386 19096936 23332865 23332492 3102961 11794159 233324...

output:

4877090544

result:

ok single line: '4877090544'

Extra Test:

score: 0
Extra Test Passed