답안 #730680

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
730680 2023-04-26T09:10:25 Z vjudge1 Izbori (COCI22_izbori) C++17
40 / 110
3000 ms 10048 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long

const int NM = 2e5, SZ = 450;

int n, a[NM+5], cnt[NM+5];
bool type[NM+5];
vector <int> v;
ll ans = 0;
int pref[NM+5];
int st[8*NM+5];

void compress(){
	for (int i = 1; i <= n; i++)
		v.push_back(a[i]);
	sort(v.begin(), v.end());
	for (int i = 1; i <= n; i++)
		a[i] = lower_bound(v.begin(), v.end(), a[i])-v.begin();
}

void solve1(){
	memset(cnt, 0, sizeof(cnt));
	for (int i = 1; i <= n; i++){
		int best = -1;
		for (int j = i; j < min(i+2*SZ, n+1); j++){
			if (type[a[j]] == 0) cnt[a[j]]++;
			if (best == -1 || cnt[a[j]] > cnt[best]) best = a[j];
			if (cnt[best]*2 > j-i+1){
				ans++;
			}
		}
		for (int j = i; j < min(i+2*SZ, n+1); j++)
			if (type[a[j]] == 0) cnt[a[j]]--;
	}
}

void update(int id, int l, int r, int i){
	if (i < l || i > r) return;
	if (l == r){
		st[id]++;
		return;
	}
	int mid = (l+r)/2;
	update(2*id, l, mid, i);
	update(2*id+1, mid+1, r, i);
	st[id] = st[2*id]+st[2*id+1];
}

int get(int id, int l, int r, int u, int v){
	if (u > v || v < l || u > r) return 0;
	if (l >= u && r <= v) return st[id];
	int mid = (l+r)/2;
	return get(2*id, l, mid, u, v)+get(2*id+1, mid+1, r, u, v);
}

void solve2(int s){
	pref[0] = 0;
	for (int i = 1; i <= n; i++){
		pref[i] = pref[i-1];
		if (a[i] == s) pref[i]++;
	}
	memset(st, 0, sizeof(st));
	update(1, 0, 2*n, n);
	for (int i = 1; i <= n; i++){
		ans += get(1, 0, 2*n, 0, 2*pref[i]-i-1+n);
		update(1, 0, 2*n, 2*pref[i]-i+n);
	}
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	compress();
	for (int i = 1; i <= n; i++)
		cnt[a[i]]++;
	for (int i = 0; i < n; i++)
		if (cnt[i] > SZ) type[i] = 1;
	solve1();
	for (int i = 0; i < n; i++)
		if (type[i]) solve2(i);
	cout << ans;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 1 ms 1128 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 2 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 1 ms 1128 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 2 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 4 ms 1108 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 9 ms 1108 KB Output is correct
10 Correct 9 ms 1108 KB Output is correct
11 Correct 10 ms 1108 KB Output is correct
12 Correct 7 ms 1108 KB Output is correct
13 Correct 10 ms 1148 KB Output is correct
14 Correct 11 ms 1148 KB Output is correct
15 Correct 10 ms 1108 KB Output is correct
16 Correct 10 ms 1108 KB Output is correct
17 Correct 9 ms 7380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 344 ms 8948 KB Output is correct
2 Correct 424 ms 9524 KB Output is correct
3 Correct 243 ms 8580 KB Output is correct
4 Correct 443 ms 9596 KB Output is correct
5 Correct 449 ms 9640 KB Output is correct
6 Correct 493 ms 9724 KB Output is correct
7 Correct 477 ms 9728 KB Output is correct
8 Correct 503 ms 9724 KB Output is correct
9 Correct 511 ms 9720 KB Output is correct
10 Correct 507 ms 9720 KB Output is correct
11 Correct 439 ms 9716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 1 ms 1128 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 2 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 4 ms 1108 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 9 ms 1108 KB Output is correct
10 Correct 9 ms 1108 KB Output is correct
11 Correct 10 ms 1108 KB Output is correct
12 Correct 7 ms 1108 KB Output is correct
13 Correct 10 ms 1148 KB Output is correct
14 Correct 11 ms 1148 KB Output is correct
15 Correct 10 ms 1108 KB Output is correct
16 Correct 10 ms 1108 KB Output is correct
17 Correct 9 ms 7380 KB Output is correct
18 Correct 344 ms 8948 KB Output is correct
19 Correct 424 ms 9524 KB Output is correct
20 Correct 243 ms 8580 KB Output is correct
21 Correct 443 ms 9596 KB Output is correct
22 Correct 449 ms 9640 KB Output is correct
23 Correct 493 ms 9724 KB Output is correct
24 Correct 477 ms 9728 KB Output is correct
25 Correct 503 ms 9724 KB Output is correct
26 Correct 511 ms 9720 KB Output is correct
27 Correct 507 ms 9720 KB Output is correct
28 Correct 439 ms 9716 KB Output is correct
29 Correct 468 ms 9724 KB Output is correct
30 Correct 241 ms 1496 KB Output is correct
31 Correct 471 ms 1748 KB Output is correct
32 Correct 1189 ms 2716 KB Output is correct
33 Correct 522 ms 1876 KB Output is correct
34 Correct 547 ms 1928 KB Output is correct
35 Correct 356 ms 1656 KB Output is correct
36 Correct 215 ms 1496 KB Output is correct
37 Correct 240 ms 1496 KB Output is correct
38 Correct 645 ms 9736 KB Output is correct
39 Correct 653 ms 9736 KB Output is correct
40 Correct 646 ms 9736 KB Output is correct
41 Correct 637 ms 9744 KB Output is correct
42 Correct 626 ms 9748 KB Output is correct
43 Correct 938 ms 9764 KB Output is correct
44 Correct 938 ms 9768 KB Output is correct
45 Correct 1018 ms 9764 KB Output is correct
46 Correct 938 ms 9756 KB Output is correct
47 Correct 914 ms 9852 KB Output is correct
48 Execution timed out 3050 ms 10048 KB Time limit exceeded
49 Halted 0 ms 0 KB -