답안 #755455

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
755455 2023-06-10T06:34:42 Z MilosMilutinovic Izbori (COCI22_izbori) C++14
40 / 110
3000 ms 2252 KB
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int M = 2 * N;
const int B = sqrt(N * 1.87);
int n, a[N], c[M], cnt[N], occ[N];
void modify(int p, int v) {
    for (p += N; p < M; p += p & -p) c[p] += v;
}
int query(int p) {
    int res = 0;
    for (p += N; p > 0; p -= p & -p) res += c[p];
    return res;
}
void compress() {
    vector<int> xs;
    for (int i = 1; i <= n; i++)
        xs.push_back(a[i]);
    sort(xs.begin(), xs.end());
    xs.erase(unique(xs.begin(), xs.end()), xs.end());
    for (int i = 1; i <= n; i++) {
        a[i] = (int) (lower_bound(xs.begin(), xs.end(), a[i]) - xs.begin()) + 1;
    }
}
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
    }
    compress();
    for (int i = 1; i <= n; i++) {
        occ[a[i]] += 1;
    }
    long long ans = 0;
    for (int v = 1; v <= n; v++) {
        if (occ[v] >= B) {
            modify(0, +1);
            int bal = 0;
            for (int i = 1; i <= n; i++) {
                bal += (a[i] == v ? 1 : -1);
                ans += query(bal - 1);
                modify(bal, +1);
            }
            modify(0, -1);
            bal = 0;
            for (int i = 1; i <= n; i++) {
                bal += (a[i] == v ? 1 : -1);
                modify(bal, -1);
            }
        }
    }
    for (int l = 1; l <= n; l++) {
        int f = a[l];
        for (int r = l; r <= min(l + 2 * B, n); r++) {
            cnt[a[r]] += 1;
            if (cnt[a[r]] > cnt[f])
                f = a[r];
            if (cnt[f] * 2 > r - l + 1 && occ[f] < B)
                ans++;
        }
        for (int r = l; r <= min(l + 2 * B, n); r++) {
            cnt[a[r]] = 0;
        }
    }
    printf("%lld", ans);
    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:26:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
Main.cpp:28:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |         scanf("%d", &a[i]);
      |         ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 12 ms 340 KB Output is correct
10 Correct 11 ms 340 KB Output is correct
11 Correct 12 ms 340 KB Output is correct
12 Correct 11 ms 340 KB Output is correct
13 Correct 11 ms 340 KB Output is correct
14 Correct 12 ms 344 KB Output is correct
15 Correct 11 ms 348 KB Output is correct
16 Correct 11 ms 340 KB Output is correct
17 Correct 12 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 933 ms 1996 KB Output is correct
2 Correct 1320 ms 2152 KB Output is correct
3 Correct 767 ms 1360 KB Output is correct
4 Correct 1382 ms 2192 KB Output is correct
5 Correct 1494 ms 2168 KB Output is correct
6 Correct 1463 ms 2252 KB Output is correct
7 Correct 1496 ms 2252 KB Output is correct
8 Correct 1529 ms 2244 KB Output is correct
9 Correct 1542 ms 2252 KB Output is correct
10 Correct 1567 ms 2252 KB Output is correct
11 Correct 1540 ms 2252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 12 ms 340 KB Output is correct
10 Correct 11 ms 340 KB Output is correct
11 Correct 12 ms 340 KB Output is correct
12 Correct 11 ms 340 KB Output is correct
13 Correct 11 ms 340 KB Output is correct
14 Correct 12 ms 344 KB Output is correct
15 Correct 11 ms 348 KB Output is correct
16 Correct 11 ms 340 KB Output is correct
17 Correct 12 ms 340 KB Output is correct
18 Correct 933 ms 1996 KB Output is correct
19 Correct 1320 ms 2152 KB Output is correct
20 Correct 767 ms 1360 KB Output is correct
21 Correct 1382 ms 2192 KB Output is correct
22 Correct 1494 ms 2168 KB Output is correct
23 Correct 1463 ms 2252 KB Output is correct
24 Correct 1496 ms 2252 KB Output is correct
25 Correct 1529 ms 2244 KB Output is correct
26 Correct 1542 ms 2252 KB Output is correct
27 Correct 1567 ms 2252 KB Output is correct
28 Correct 1540 ms 2252 KB Output is correct
29 Correct 1483 ms 2252 KB Output is correct
30 Correct 285 ms 852 KB Output is correct
31 Correct 570 ms 1240 KB Output is correct
32 Correct 1336 ms 2124 KB Output is correct
33 Correct 596 ms 1396 KB Output is correct
34 Correct 614 ms 1308 KB Output is correct
35 Correct 394 ms 1024 KB Output is correct
36 Correct 227 ms 804 KB Output is correct
37 Correct 296 ms 880 KB Output is correct
38 Correct 1482 ms 2252 KB Output is correct
39 Correct 1425 ms 2252 KB Output is correct
40 Correct 1424 ms 2252 KB Output is correct
41 Correct 1484 ms 2252 KB Output is correct
42 Correct 1418 ms 2252 KB Output is correct
43 Correct 1508 ms 2244 KB Output is correct
44 Correct 1522 ms 2116 KB Output is correct
45 Correct 1529 ms 2252 KB Output is correct
46 Correct 1512 ms 2232 KB Output is correct
47 Correct 1423 ms 2116 KB Output is correct
48 Execution timed out 3055 ms 2252 KB Time limit exceeded
49 Halted 0 ms 0 KB -