CSES - Datatähti 2019 alku - Results
Submission details
Task:Taulukko
Sender:ufokurpitsa
Submission time:2018-10-08 19:21:25 +0300
Language:C++
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:5:5: error: 'ios_base' has not been declared
     ios_base::sync_with_stdio(0);
     ^~~~~~~~
input/code.cpp:6:5: error: 'cin' was not declared in this scope
     cin.tie(0);
     ^~~
input/code.cpp:6:5: note: suggested alternative:
In file included from input/code.cpp:1:0:
/usr/include/c++/7/iostream:60:18: note:   'std::cin'
   extern istream cin;  /// Linked to standard input
                  ^~~
input/code.cpp:20:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 if (m.size() > k) {
                     ~~~~~~~~~^~~

Code

#include <iostream>
#include <map>
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
int k;
std::cin >> n >> k;
int x[n];
for(int i=0;i<n;i++){
std::cin >> x[i];
}
std::map<int, int> m;
long r = 0;
int j = 0;
for(int i=0;i<n;i++) {
if (j <= n) {
for(;;) {
if (m.size() > k) {
r += j-i-1;
break;
} else if (j < n) {
m[x[j]]++;
j++;
} else {
r += j-i;
j++;
break;
}
}
} else {
r += n-i;
}
m[x[i]]--;
if (m[x[i]] == 0) {
m.erase(x[i]);
}
}
std::cout << r << '\n';
return 0;
}