Cod sursa(job #881466)

Utilizator wzrdwzrd tst wzrd Data 18 februarie 2013 00:19:39
Problema Ghiozdan Scor 24
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <iostream>
#include <fstream>
#define DG 75555
#define DO 205
#define MULT (1<<30)
using namespace std;

int n,g,fr[DO],bst[DO][DG],gmax,nmin;
int dq[DG],ind[DG],p=1,q=0;

int main()
{
    ifstream f("ghiozdan.in");
    ofstream out("ghiozdan.out");
    f>>n>>g;
    for(int i=0; i<n; ++i) {
      int x;
      f>>x;
      ++fr[x];
    }
    for(int i=0; i<DO; ++i) for(int j=0; j<=g; ++j) bst[i][j]=MULT;
    bst[0][0]=0;
    for(int i=1; i<DO; ++i) for(int gstart=0; gstart<i; ++gstart) {
      p=1; q=0;
      for(int j=gstart; j<=g; j+=i) {
        for(;p<=q && ind[p]<j-fr[i]*i; ++p);
        for(;p<=q && dq[q]>=bst[i-1][j]-j/i;--q);
        dq[++q]=bst[i-1][j]-j/i;
        ind[q]=j;

        bst[i][j]=bst[i-1][ind[p]]+(j-ind[p])/i;
        if(bst[i][j]!=MULT) {
          if(gmax<j) gmax=j,nmin=bst[i][j];
          else if(gmax==j) nmin=min(nmin,bst[i][j]);
        }
      }
    }


    out<<gmax<<' '<<nmin<<'\n';
    return 0;
}