CSES - Datatähti 2019 alku - Results
Submission details
Task:Taulukko
Sender:borkbork
Submission time:2018-10-06 19:57:05 +0300
Language:C++
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:45:24: warning: unused variable 'count' [-Wunused-variable]
     unsigned long long count=0;
                        ^~~~~
input/code.cpp:68:1: error: expected '}' at end of input
 }
 ^

Code

#include<iostream>
#include<unordered_set>
#include<unordered_map>

int main(){
    std::ios_base::sync_with_stdio(0);std::cin.tie(NULL);
    unsigned int n,k;
    std::cin>>n>>k;
    unsigned int d[500100];
    unsigned int input;
    std::unordered_set<unsigned int> s;
    for(size_t i=0;i<n;i++){std::cin>>input;s.insert(input);d[i]=input;}
    unsigned int uniq=s.size();
    unsigned long long res=n;
    for(size_t i=1;i<k;i++){res+=(n-i);}
    if(n==k){std::cout<<res;return 0;}
    if(uniq<1+k){for(size_t i=k;i<n;i++){res+=(n-i);}std::cout<<res;return 0;}
    if(n/2<k||n<101){

    unsigned long long count=0;
    std::unordered_map<unsigned int,unsigned int> map;
    
    for(size_t i=0;i<n-k+1;i++){
        count=1;
        map.clear();
        map[d[i]]++;
        for(size_t j=1;j<n-i;j++){
            
            if(!map[d[i+j]]){
                count++;
                map[d[i+j]]++;
            }
            if(count>k){break;}
            if(k<j+1){
                if(count<1+k){
                    res++;
                    
                }
            }
        }
    }

    }
    else{
    unsigned long long count=0;
    std::unordered_map<unsigned int,unsigned int> map;
    size_t i=0;
    size_t size=n-2;
    for(;i<n;){
        if(n-k-1<i){break;}
        if(n-i<size){size=n-i;}
        map.clear();
        for(size_t j=0;j<size;j++){map[d[i+j]]++;}
        if(map.size()<k+1){res+=((size+1)*size/2);i=i+size-k;}
        else{
            while(k<size){
                if(map[d[i+size]]<2){map.erase(d[i+size]);}
                else{map[d[i+size]]--;}
                size--;
                if(map.size()<k+1){res+=((size+1)*size/2);i=i+size-k;break;}
            }
        }


        i++;
    }
    std::cout<<res;
}