This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
ll sum(ll l, ll r) {
// l^2 + ... + r^2
l--;
return r * (r + 1) * (2 * r + 1) / 6 - l * (l + 1) * (2 * l + 1) / 6;
}
int brute(int n, vector<int> &a) {
int ans = 0;
for (int i = 0; i < n; i++) {
vector<int> cnt(n);
int mx = 0;
for (int j = i; j < n; j++) {
cnt[a[j]]++;
mx = max(mx, cnt[a[j]]);
if (2 * mx > (j - i + 1)) {
ans++;
}
}
}
return ans;
}
void solve() {
int n;
cin >> n;
map<int, int> mp;
vector<int> cnt, a(n);
{
for (int &x: a) {
cin >> x;
mp[x];
}
int z = 0;
for (auto &it: mp) {
it.second = z++;
}
cnt.assign(mp.size(), 0);
for (int &x: a) {
x = mp[x];
cnt[x]++;
}
}
cout << brute(n, a);
return;
vector<vector<int>> pos(mp.size());
for (int i = 0; i < n; i++) {
pos[a[i]].emplace_back(i);
}
int N = 100;
ll ans = 0;
for (int i = 0; i < mp.size(); i++) {
if (cnt[i] <= N) {
cout << i << endl;
for (int l = 0; l < cnt[i]; l++) {
for (int r = l; r < cnt[i]; r++) {
int left = 0, right = n - 1, plus = r - l + 1;
int have = plus - ((pos[i][r] - pos[i][l] + 1) - plus);
if (have <= 0) {
continue;
}
if (l > 0) {
left = pos[i][l - 1] + 1;
}
if (r + 1 < cnt[i]) {
right = pos[i][r + 1] - 1;
}
right = min(right, pos[i][r] + (have - 1));
left = max(left, pos[i][l] - (have - 1));
int right2 = left + (2 * plus - 1) - 1;
cout << l << ' ' << r << ' ' << left << ' ' << right << ' ' << right2 << endl;
// right - 1
// right-1 - 2
// ...
// right2 - da stop
// qogani bir hil
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
int queries = 1;
#ifdef test_cases
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
cin >> queries;
#else
// cin >> queries;
#endif
for (int test_case = 1; test_case <= queries; test_case++) {
#ifdef test_cases
cout << "Test case: " << test_case << '\n';
#endif
solve();
cout << '\n';
}
}
Compilation message (stderr)
Main.cpp: In function 'void solve()':
Main.cpp:56:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::map<int, int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for (int i = 0; i < mp.size(); i++) {
| ~~^~~~~~~~~~~
Main.cpp:55:8: warning: unused variable 'ans' [-Wunused-variable]
55 | ll ans = 0;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |