CSES - Datatähti 2019 alku - Results
Submission details
Task:Taulukko
Sender:jaakkonen
Submission time:2018-10-08 17:00:52 +0300
Language:C++
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:37:50: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(set<long> (arr.begin(), arr.end()).size() <= k){
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
input/code.cpp:39:9: error: return-statement with no value, in function returning 'int' [-fpermissive]
         return;
         ^~~~~~
input/code.cpp:41:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while (index != arr.size()){
            ~~~~~~^~~~~~~~~~~~~
input/code.cpp:50:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             if(active_nums.size() > k){
                ~~~~~~~~~~~~~~~~~~~^~~
input/code.cpp:19:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%ld",&n);
     ~~~~~^~~~~~~~~~
input/code.cpp:20:10: warning: ignoring return value...

Code

#include<iostream>
#include<sstream>
#include<deque>
#include<string>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
int main(){
    ios_base::sync_with_stdio(false);

    deque<long> arr;
    long k;
    long n;
    //k = 3;
    //n = 8;
    //arr = {1, 2, 1, 1, 1, 3, 1, 4};
    
    scanf("%ld",&n);
    scanf("%ld",&k);
    
    long temp;
    for(int i=0;i<n;i++){
        scanf("%ld",&temp);
        arr.push_back(temp);
    }   
    
    arr.resize(n);
    deque<long> active_nums;
    deque<long>::iterator it;
    long subarrays = 0;
    long index = 0;
    long curr_num;
    long num_to_remove;
    long removed_index;
    map<long, long> last_index;
    if(set<long> (arr.begin(), arr.end()).size() <= k){
        cout << arr.size()*(arr.size()+1)/2 << endl;
        return;
    }
    while (index != arr.size()){
        curr_num = arr[index];
        it = find(active_nums.begin(), active_nums.end(), curr_num);
        last_index[curr_num] = index;
        if(it != active_nums.end()){
            active_nums.erase(it);
            active_nums.push_back(curr_num);
        }else{
            active_nums.push_back(curr_num);
            if(active_nums.size() > k){
                num_to_remove = active_nums.front();
                //for(long x : active_nums) {num_to_remove = x; break;}
                //cout << "For-loop: " << num_to_remove << endl;
                active_nums.pop_front();
                removed_index = last_index[num_to_remove];
                //removed_index = arr.size() - (find(arr.rbegin() + arr.size() - index, arr.rend(), num_to_remove) - arr.rbegin()) - 1;
                //cout << "Current: " << removed_index << endl << "Magic: " << last_index[num_to_remove] << endl;
                subarrays += (removed_index*(2*index-removed_index-1)+2*index)/2;
                arr.erase(arr.begin(), arr.begin()+removed_index+1);
                last_index.clear();
                index = 0;
            }
        }
        index++;
    }
    subarrays += (arr.size()+1)*arr.size()/2;
    cout << subarrays << endl;
}