Cod sursa(job #596007)

Utilizator darrenRares Buhai darren Data 15 iunie 2011 10:53:35
Problema Partitie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>
#include <algorithm>
#include <queue>

using namespace std;

int N, D;
int A[300002], pos[300002];

int where[300002];
queue<int> Q;

bool compare(const int& i1, const int& i2)
{
    return A[i1] < A[i2];
}

int main()
{
    ifstream fin("partitie.in");
    ofstream fout("partitie.out");

    fin >> N >> D;
    for (int i = 1; i <= N; ++i)
    {
        fin >> A[i];
        pos[i] = i;
    }

    sort(pos + 1, pos + N + 1, compare);

    where[pos[1]] = 1;
    Q.push(pos[1]);

    for (int i = 2; i <= N; ++i)
    {
        if (A[Q.front()] + D <= A[pos[i]])
        {
            where[pos[i]] = where[Q.front()];
            Q.pop();
            Q.push(pos[i]);
        }
        else
        {
            where[pos[i]] = Q.size() + 1;
            Q.push(pos[i]);
        }
    }

    fout << Q.size() << '\n';
    for (int i = 1; i <= N; ++i)
        fout << where[i] << '\n';

    fin.close();
    fout.close();
}