Cod sursa(job #2246361)

Utilizator alex.cojocaruAlex Cojocaru alex.cojocaru Data 26 septembrie 2018 23:38:27
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.9 kb
#include <iostream>
#include <stdio.h>
#include <vector>

#define MOD 666013
#define NMAX 1100000

using namespace std;

vector <int> has [ MOD + 1 ] ;
int v [ NMAX + 2 ] ;

int H (int x ) { return x % MOD ; }

void add (int x ) {
  has[H(x)].push_back(x) ;
}

void delet (int x ) {
  int i = H (x) ;
  vector <int>::iterator j ;
  for (j = has[i].begin() ; j != has[i].end() && *j != x ; j++ ) ;
  if (j != has[i].end())
    has[i].erase(j) ;
}

bool exist (int x) { /// daca exista x cu restul i
  int i = H (x) ;
  vector <int>::iterator j ;
  for (j = has[i].begin() ; j != has[i].end() && *j != x ; j++ )
    printf ("%d ", *j ) ;
  if (j != has[i].end())
    return true ;
  return false ;
}

int main() {

  FILE *fin, *fout ;
  fin = fopen ("secv5.in", "r" ) ;
  fout = fopen ("secv5.out", "w" ) ;
  int i, j, n, u, l, count, sol1, sol2 ;
  fscanf (fin, "%d%d%d", &n, &l, &u ) ;
  for (i = 0 ; i < n ; i++ )
    fscanf (fin, "%d", &v[i] ) ;
  i = 0 ;
  j = 1 ;
  add (v[0]) ;
  count = 1 ;
  sol1 = 0 ;
  while (j < n ) {
    if (exist(v[j]) == false ) {
      if (count+1 <= u ) {
        add (v[j]) ;
        sol1 += (j - i ) ;
        j++;
        count++;
      }
      else {
        if (v[i] == v[j] )
          count--;
        delet(v[i]) ;
        i++;
      }
    }
    else {
      sol1 += (j - i ) ;
      j++;
    }
  }
  while (i < j ) {
    delet(v[i]) ;
    i++;
  }
  i = 0 ;
  j = 1 ;
  add (v[0]) ;
  count = 1 ;
  sol2 = 0 ;
  while (j < n ) {
    if (exist(v[j]) == false ) {
      if (count+1 < l ) {
        add (v[j]) ;
        sol2 += (j - i ) ;
        j++;
        count++;
      }
      else {
        if (v[i] == v[j] || i+1 == j )
          count--;
        delet(v[i]) ;
        i++;
      }
    }
    else {
      sol2 += (j - i ) ;
      j++;
    }
  }
  fprintf (fout, "%d", sol1-sol2 ) ;
  return 0;
}