Cod sursa(job #1774209)

Utilizator TimitocArdelean Andrei Timotei Timitoc Data 8 octombrie 2016 18:06:36
Problema Ghiozdan Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <iostream>
#include <cstdio>
#define MAXG 75050
#define SIGMA 210
#define inf 0x3f3f3f3f

using namespace std;

int n, g;
int din[2][MAXG];
int c[SIGMA];

void read()
{
    scanf("%d %d", &n, &g);
    int x;
    for (int i = 1; i <= n; i++) {
		scanf("%d", &x);
		c[x]++;
    }
}

int deck[MAXG], st, dr;

int cost(int crt, int prev, int now, int pas)
{
    return din[crt][prev] + (now-prev) / pas ;
}

void solve()
{
    int crt = 1;
    for (int i = 1; i < MAXG; i++)
		din[1][i] = din[0][i] = inf;

    for (int i = 1; i < SIGMA; i++) {
		if (!c[i]) continue;
		crt ^= 1;
        for (int j = 0; j < i; j++) {
			st = 0, dr = -1;
            deck[++dr] = j;
			for (int ind = i+j, k = 0; ind <= g; ind += i, k++) {
                while (st <= dr && cost(!crt, ind, ind, i) <= cost(!crt, deck[dr], ind, i))
                    dr--;
                deck[++dr] = ind;
                while ((ind-deck[st])/i > c[i])
					st++;
                int bi = cost(!crt, deck[st], ind, i);
                din[crt][ind] = bi;
			}
        }
    }
    for (int i = g; i >= 1; i--)
		if (din[crt][i] != inf) {
			printf("%d %d\n", i, din[crt][i]);
            break;
		}
}

int main()
{
    freopen("ghiozdan.in", "r", stdin);
    freopen("ghiozdan.out", "w", stdout);

    read();
    solve();

    return 0;
}