Nu aveti permisiuni pentru a descarca fisierul grader_test16.in

Cod sursa(job #3245280)

Utilizator horia.boeriuBoeriu Horia Andrei horia.boeriu Data 28 septembrie 2024 11:28:37
Problema Secventa 5 Scor 100
Compilator c-64 Status done
Runda Arhiva de probleme Marime 2.5 kb
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAXN 1048576
unsigned int v[MAXN];
int poz[MAXN];
int f[MAXN + 1], f2[MAXN + 1];
unsigned int readUnsignedInt(FILE *fin) {
    unsigned int x;
    char ch;
    ch = fgetc(fin);
    while (isspace(ch)) {
        ch = fgetc(fin);
    }
    x = 0;
    while (isdigit(ch)) {
        x = x * 10 + ch - '0';
        ch = fgetc(fin);
    }
    return x;
}
void myqsort(int st, int dr) {
    int b, e, pivot, aux;
    b = st;
    e = dr;
    pivot = poz[(st + dr) / 2];
    while (v[poz[b]] < v[pivot]) {
        b++;
    }
    while (v[poz[e]] > v[pivot]) {
        e--;
    }
    while (b < e) {
        aux = poz[b];
        poz[b] = poz[e];
        poz[e] = aux;
        do {
            b++;
        } while (v[poz[b]] < v[pivot]);
        do {
            e--;
        } while (v[poz[e]] > v[pivot]);
    }
    if (st < e) {
        myqsort(st, e);
    }
    if (e + 1 < dr) {
        myqsort(e + 1, dr);
    }
}
int main()
{
    FILE *fin, *fout;
    int n, i, st, dr, j, k, nrj, nrk, x;
    unsigned int pr;
    long long rez;
    fin = fopen("secv5.in", "r");
    fscanf(fin, "%d%d%d ", &n, &st, &dr);
    for (i = 0; i < n; i++) {
        v[i] = readUnsignedInt(fin);
        poz[i] = i;
    }
    fclose(fin);
    //normalizez numerele, sortez numerele cu vectorul poz
    myqsort(0, n - 1);
    pr = v[poz[0]];
    v[poz[0]] = x = 1;
    for (i = 1; i < n; i++) {
        if (v[poz[i]] > pr) {
            x++;
            pr = v[poz[i]];
        }
        v[poz[i]] = x;
    }

    //pentru fiecare pozitie de inceput gasesc cu 2 pointers sfarsitul secventei ca sa fie st diferite si sfarsitul ca sa fie dr + 1 diferite
    //pot sa fac parcuregrile separat
    j = k = nrj = nrk = rez = 0;
    for (i = 0; i < n; i++) {
        while (j < n && nrj < st) {
            nrj += (f[v[j]] == 0);
            f[v[j]]++;
            j++;
        }
        while (k < n && nrk <= dr) {
            nrk += (f2[v[k]] == 0);
            f2[v[k]]++;
            k++;
        }
        if (nrk <= dr && k == n) {
            k++;
        }
        //in k - 1 este primul care nu mai face parte din secventa cu dr diferite
        if (nrj == st) {
            rez += k - j;
        }
        f[v[i]]--;
        nrj -= (f[v[i]] == 0);
        f2[v[i]]--;
        nrk -= (f2[v[i]] == 0);
    }
    fout = fopen("secv5.out", "w");
    fprintf(fout, "%lld\n", rez);
    fclose(fout);
    return 0;
}