답안 #730677

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

#define ll long long

const int NM = 2e5, SZ = 320;

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 1108 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 1 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 1108 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 1 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 3 ms 1108 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 8 ms 1148 KB Output is correct
10 Correct 7 ms 1148 KB Output is correct
11 Correct 8 ms 1148 KB Output is correct
12 Correct 8 ms 1108 KB Output is correct
13 Correct 7 ms 1148 KB Output is correct
14 Correct 8 ms 1144 KB Output is correct
15 Correct 6 ms 1108 KB Output is correct
16 Correct 9 ms 1148 KB Output is correct
17 Correct 7 ms 7380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 264 ms 8952 KB Output is correct
2 Correct 361 ms 9864 KB Output is correct
3 Correct 191 ms 8776 KB Output is correct
4 Correct 418 ms 9952 KB Output is correct
5 Correct 381 ms 10020 KB Output is correct
6 Correct 417 ms 10128 KB Output is correct
7 Correct 392 ms 10124 KB Output is correct
8 Correct 390 ms 10116 KB Output is correct
9 Correct 420 ms 10120 KB Output is correct
10 Correct 406 ms 10132 KB Output is correct
11 Correct 347 ms 10128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 1 ms 1108 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 1 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 3 ms 1108 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 8 ms 1148 KB Output is correct
10 Correct 7 ms 1148 KB Output is correct
11 Correct 8 ms 1148 KB Output is correct
12 Correct 8 ms 1108 KB Output is correct
13 Correct 7 ms 1148 KB Output is correct
14 Correct 8 ms 1144 KB Output is correct
15 Correct 6 ms 1108 KB Output is correct
16 Correct 9 ms 1148 KB Output is correct
17 Correct 7 ms 7380 KB Output is correct
18 Correct 264 ms 8952 KB Output is correct
19 Correct 361 ms 9864 KB Output is correct
20 Correct 191 ms 8776 KB Output is correct
21 Correct 418 ms 9952 KB Output is correct
22 Correct 381 ms 10020 KB Output is correct
23 Correct 417 ms 10128 KB Output is correct
24 Correct 392 ms 10124 KB Output is correct
25 Correct 390 ms 10116 KB Output is correct
26 Correct 420 ms 10120 KB Output is correct
27 Correct 406 ms 10132 KB Output is correct
28 Correct 347 ms 10128 KB Output is correct
29 Correct 353 ms 10612 KB Output is correct
30 Correct 176 ms 1876 KB Output is correct
31 Correct 367 ms 2324 KB Output is correct
32 Correct 869 ms 3620 KB Output is correct
33 Correct 368 ms 2340 KB Output is correct
34 Correct 395 ms 2328 KB Output is correct
35 Correct 257 ms 1932 KB Output is correct
36 Correct 156 ms 1644 KB Output is correct
37 Correct 186 ms 1752 KB Output is correct
38 Correct 619 ms 10128 KB Output is correct
39 Correct 589 ms 10124 KB Output is correct
40 Correct 554 ms 10228 KB Output is correct
41 Correct 605 ms 10232 KB Output is correct
42 Correct 543 ms 10128 KB Output is correct
43 Correct 907 ms 10660 KB Output is correct
44 Correct 967 ms 10660 KB Output is correct
45 Correct 898 ms 10736 KB Output is correct
46 Correct 825 ms 10656 KB Output is correct
47 Correct 845 ms 10676 KB Output is correct
48 Execution timed out 3034 ms 10608 KB Time limit exceeded
49 Halted 0 ms 0 KB -