Cod sursa(job #1627263)

Utilizator Athena99Anghel Anca Athena99 Data 3 martie 2016 15:54:37
Problema Partitie Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <algorithm>
#include <fstream>
#include <queue>
#include <string>

using namespace std;

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

const int nmax= 300000;

struct str {
    int x, y;
};

struct str_cmp {
    bool operator () ( const str &x, const str &y ) {
        return x.x>y.x;
    }
};

str x[nmax+1];
priority_queue <str, vector <str>, str_cmp> last;

bool cmp1( str x, str y ) {
    return x.x<y.x;
}

bool cmp2( str x, str y ) {
    return x.y<y.y;
}

inline str mp( int x, int y ) {
    str sol;
    sol.x= x, sol.y= y;

    return sol;
}

string buffer;
string::iterator buffer_it;

void read_int_nn( int &x ) {
    for ( ; *buffer_it<'0' || *buffer_it>'9'; ++buffer_it ) ;
    for ( x= 0; *buffer_it>='0' && *buffer_it<='9'; ++buffer_it ) {
        x= x*10+*buffer_it-'0';
    }
}

int main(  ) {
    getline( fin, buffer, (char)0 );
    buffer_it= buffer.begin();

    int n, d;
    read_int_nn(n), read_int_nn(d);
    for ( int i= 1; i<=n; ++i ) {
        read_int_nn(x[i].x);
        x[i].y= i;
    }
    sort( x+1, x+n+1, cmp1 ) ;

    int k= 0;
    for ( int i= 1; i<=n; ++i ) {
        if ( !last.empty() ) {
            str nr= last.top();
            if ( nr.x+d<=x[i].x ) {
                last.pop();
                nr.x= x[i].x;
                x[i].x= nr.y;
                last.push(nr);
                continue;
            }
        }
        ++k;
        last.push(mp(x[i].x, k));
        x[i].x= k;
    }

    sort( x+1, x+n+1, cmp2 ) ;
    fout<<k<<"\n";
    for ( int i= 1; i<=n; ++i ) {
        fout<<x[i].x<<"\n";
    }

    return 0;
}