Cod sursa(job #581683)

Utilizator darrenRares Buhai darren Data 14 aprilie 2011 14:57:16
Problema Progresii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <fstream>

using namespace std;

int n, m, k, l;
int d[100002], v[100002];
int tot;

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

	fin >> n >> m >> k >> l;
	for (int i = 1; i <= n; ++i)
	{
		fin >> d[i];
		d[i] *= -1, d[i] += l;

		tot += d[i] + 1;
		v[i] = 1;
	}

	int step;
	for (step = 1; (step << 1) <= m; step <<= 1);

	for (int i = n; i >= 1 && tot > k; --i)
	{
		int stepx = step, res, hnow = d[i];

		for (res = 0; stepx; stepx >>= 1)
            if (res + stepx >= 1 && tot - d[i] + (d[i] / (res + stepx)) >= 0 && (res + stepx == 1 || d[i] / (res + stepx) != d[i] / (res + stepx - 1)))
                res += stepx;

        tot -= d[i];
        tot += (d[i] / (res - stepx));
        v[i] = res;
	}
	if (tot > k) fout << -1 << '\n';
	else
		for (int i = 1; i <= n; ++i)
			fout << v[i] << '\n';

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