Submission #866226

# Submission time Handle Problem Language Result Execution time Memory
866226 2023-10-25T15:41:34 Z TAhmed33 Izbori (COCI22_izbori) C++
0 / 110
14 ms 8664 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll trig (ll x) { return x * (x + 1) / 2; }
ll get2 (ll a, ll b, ll c) {
	a = min(a, c); b = min(b, c);
	c = min(c, a + b);
	return (c - b) * (b + 1) + (c + 1) * (a - c + b + 1) - trig(a) + trig(c - b - 1);
}
const int MAXN = 2e5 + 25;
int arr[MAXN], n;
vector <int> occ[MAXN];
vector <int> d;
int a[2 * MAXN + 25];
ll solve1 (int x) {
	ll ret = 0; int cur = 0; int sum = 0; a[MAXN] = 1;
	for (int i = 1; i <= n; i++) {
		if (arr[i] == x) {
			sum++; cur += a[sum - 1 + MAXN];
		} else {
			cur -= a[sum - 1 + MAXN]; sum--;
		}
		ret += cur; a[sum + MAXN]++;
	}
	return ret;
}
ll solve2 (int x) {
	ll ret = (int)occ[x].size() - 2;
	for (int i = 1; i < (int)occ[x].size() - 2; i--) {
		for (int j = i + 1; j < (int)occ[x].size() - 1; j++) {
			ret += get2(occ[x][i] - occ[x][i - 1], occ[x][j + 1] - occ[x][j], 2 * (j - i + 1) - 2 - occ[x][j] + occ[x][i]);
		}
	}
	return ret;
}
int main () {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> arr[i];
		d.push_back(arr[i]);
	}
	sort(d.begin(), d.end()); d.resize(unique(d.begin(), d.end()) - d.begin());
	for (int i = 1; i <= n; i++) arr[i] = lower_bound(d.begin(), d.end(), arr[i]) - d.begin();
	for (int i = 1; i <= n; i++) {
		occ[arr[i]].push_back(i);
	}
	ll sum = 0;
	int x = sqrt(n); while (x * x < n) x++; while (x * x > n) x--;
	for (int i = 0; i < n; i++) {
		if (occ[i].empty()) continue;
		if ((int)occ[i].size() > x) sum += solve1(i);
		else {
			occ[i].push_back(n + 1);
			occ[i].insert(occ[i].begin(), 0);
			sum += solve2(i);
		}
	}
	cout << sum << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Incorrect 1 ms 6492 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Incorrect 1 ms 6492 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 8664 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Incorrect 1 ms 6492 KB Output isn't correct
4 Halted 0 ms 0 KB -