답안 #730663

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

#define ll long long

const int NM = 1e5, SZ = 320;

int n, a[NM+5], cnt[NM+5];
bool type[NM+5];
vector <int> v;
ll ans = 0;
int pref[NM+5], bit[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 p){
	while (p <= n){
		bit[p]++;
		p += p & (-p);
	}
}

int get(int p){
	int res = 0;
	while (p > 0){
		res += bit[p];
		p -= p & (-p);
	}
	return res;
}

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]++;
	}
	for (int i = 1; i <= n; i++){
		if (2*pref[i]-i > 0) ans++;
		ans += get(2*pref[i]-i-1);
		update(2*pref[i]-i);
	}
}

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 724 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 716 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 716 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 720 KB Output is correct
7 Correct 3 ms 720 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 8 ms 724 KB Output is correct
10 Correct 8 ms 724 KB Output is correct
11 Correct 8 ms 744 KB Output is correct
12 Correct 8 ms 768 KB Output is correct
13 Correct 9 ms 724 KB Output is correct
14 Correct 8 ms 768 KB Output is correct
15 Correct 9 ms 764 KB Output is correct
16 Correct 9 ms 724 KB Output is correct
17 Correct 6 ms 764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 9 ms 1368 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 716 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 720 KB Output is correct
7 Correct 3 ms 720 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 8 ms 724 KB Output is correct
10 Correct 8 ms 724 KB Output is correct
11 Correct 8 ms 744 KB Output is correct
12 Correct 8 ms 768 KB Output is correct
13 Correct 9 ms 724 KB Output is correct
14 Correct 8 ms 768 KB Output is correct
15 Correct 9 ms 764 KB Output is correct
16 Correct 9 ms 724 KB Output is correct
17 Correct 6 ms 764 KB Output is correct
18 Incorrect 9 ms 1368 KB Output isn't correct
19 Halted 0 ms 0 KB -