Submission #893555

#TimeUsernameProblemLanguageResultExecution timeMemory
893555vjudge1Izbori (COCI22_izbori)C++17
40 / 110
54 ms11216 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()

const int N = (int)2e5 + 5;
const ll mod = (int)1e9 + 7;
const ll inf = (int)(1e9) + 100;

map<int, int> ma;
int cnt[N], n, a[N];
bool cs3 = 1;


void sub3() {
    map<int, int> CNT;
    ll pref = 0, ans = 0;
    CNT[0]++;
    for (int i = 1; i <= n; i++) {
        if (a[i] == 2) pref += 1;
        else pref -= 1;
        ans += CNT[pref];
        CNT[pref]++;
    }
    cout << (n * 1LL * (n + 1)) / 2 - ans;
}

void solve() {
    cin >> n;
    vector<int> v;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        v.pb(a[i]);
        if (a[i] > 2) cs3 = 0;
    }
    sort(all(v));
    v.erase(unique(all(v)), v.end());
    int timer = 0;
    for (auto i : v) {
        ma[i] = ++timer;
    }
    if (cs3) {
        sub3();
        return;
    }
    for (int i = 1; i <= n; i++) a[i] = ma[a[i]];
    if (n <= 2000) {
        int ans = 0;
        for (int i = 1; i <= n; i++) {
                int mx = 0;
            for (int j = 1; j <= n; j++) {
                cnt[j] = 0;
            }
            for (int j = i; j <= n; j++) {
                cnt[a[j]]++;
                mx = max(mx, cnt[a[j]]);
                if (mx >= (j - i + 1) / 2 + 1) ans++;

            }
        }
        cout << ans;
        return;
    }
}

signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int T = 1;
    //cin >> T;
    while (T--) solve();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...