Cod sursa(job #999841)

Utilizator maritimCristian Lambru maritim Data 21 septembrie 2013 15:50:24
Problema Partitie Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.91 kb
#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;

ifstream f("partitie.in");
ofstream g("partitie.out");

struct nr
{
    int info,poz;
} ;

typedef struct Compare
{
    bool operator() (const nr a,const nr b)
    {
        return a.info < b.info;
    }
} ICompare;

#define MaxN 400100

int N,L,Sol;
int SolV[MaxN];
nr A[MaxN];

void citire(void)
{
    f >> N >> L;
    for(int i=1;i<=N;A[i].poz = i++)
        f >> A[i].info;
}

void Rezolvare(void)
{
    int poz = 1;

    sort(A+1,A+N+1,ICompare());

    SolV[A[1].poz] = Sol = 1;

    for(int i=2;i<=N;i++)
    {
    //    cout << A[i].info << " " << A[poz].info << "\n";
        if(A[i].info >= A[poz].info+L)
            SolV[A[i].poz] = SolV[A[poz++].poz];
        else
            SolV[A[i].poz] = ++ Sol;
    }
}

int main()
{
    citire();
    Rezolvare();

    g << Sol << "\n";
    for(int i=1;i<=N;i++)
        g << SolV[i] << "\n";
}