hiho week 241 register

Ended

Participants:117

Verdict:Accepted
Score:100 / 100
Submitted:2019-02-16 17:15:29

Lang:G++

Edit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 200005;
int n, a[N];
long long k;
long long check(long long mid) {
    long long maxValue = 0;
    int l = 1;
    long long sum = 0;
    unordered_map<int, int> cnt;
    for (int r = 1; r <= n; ++r) {
        int s = cnt[a[r]];
        maxValue += s;
        ++cnt[a[r]];
        if (sum > mid) {
            while (maxValue > mid && l < r) {
                s = --cnt[a[l]];
                maxValue -= s;
                ++l;
            }
        }
        sum += (r - l + 1);
        if (sum > k) {
            return sum;
        }
    }
    return sum;
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX