Cod sursa(job #2501212)

Utilizator Iulia25Hosu Iulia Iulia25 Data 29 noiembrie 2019 11:27:48
Problema Partitie Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <fstream>
#include <algorithm>
#include <set>

using namespace std;

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

int n, d, nr;
int ans[300005];
pair <int, int> a[300005];

multiset <pair <int, int> > s;

int main()	{
  fin >> n >> d;
  for (int i = 1; i <= n; ++i)
		fin >> a[i].first, a[i].second = i;
	sort (a + 1, a + n + 1);
  for (int i = 1; i <= n; ++i)	{
    pair <int, int> x = {d - a[i].first, 1};
    auto it = s.lower_bound(x);
    if (it == s.end())	{
      s.insert({- a[i].first, ++nr});
      ans[a[i].second] = nr;
      continue;
    }
    ans[a[i].second] = it -> second;
    s.erase(it);
    s.insert({d - a[i].first, it -> second});
  }
  fout << nr << '\n';
  for (int i = 1; i <= n; ++i)
		fout << ans[i] << '\n';
	return 0;
}