CSES - Datatähti 2019 alku - Results
Submission details
Task:Taulukko
Sender:intoo
Submission time:2018-10-10 20:24:25 +0300
Language:C++
Status:COMPILE ERROR

Compiler report

input/code.cpp:6:26: error: expected constructor, destructor, or type conversion before '(' token
 ios_base::sync_with_stdio(0);
                          ^
input/code.cpp:7:1: error: 'cin' does not name a type
 cin.tie(0);
 ^~~
input/code.cpp: In function 'int main()':
input/code.cpp:26:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if (z.size() > k) {
             ~~~~~~~~~^~~
input/code.cpp:31:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 if (q.size() == k) break;
                     ~~~~~~~~~^~~~

Code

#include <iostream>
#include <unordered_set>

using namespace std;

ios_base::sync_with_stdio(0);
cin.tie(0);

long long t[505050][2], l[505050];

int main() {
    long long n, k, m = 0, a;
    cin >> n >> k;
    for (long long i = 0; i < n; i++) {
        cin >> a;
        if (t[m-1][0] != a) {
            t[m++][0] = a;
        }
        t[m-1][1]++;
    }
    long long s = 0;
    long long x = 0, o = 0;
    unordered_set<long long> z;
    for (long long i = 0; i < m; i++) {
        z.insert(t[i][0]);
        if (z.size() > k) {
            unordered_set<long long> q;
            for (long long j = i-1; j >= 0; j--) {
                i = j;
                q.insert(t[j][0]);
                if (q.size() == k) break;
                l[o] += t[j][1];
            }
            o++;
            s += x*((x+1)/2);
            x = 0;
            z.clear();
            continue;
        }
        x += t[i][1];
    }
    s += x*(x+1)/2;
    for (long long i = 0; i < o; i++) s -= l[i]*((l[i]+1)/2);
    cout << s << endl;
}