Cod sursa(job #1413644)

Utilizator teoionescuIonescu Teodor teoionescu Data 1 aprilie 2015 23:52:13
Problema Ghiozdan Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#include<fstream>
#include<cmath>
using namespace std;
ifstream in("ghiozdan.in");
ofstream out("ghiozdan.out");
const int INF = 0x3f3f3f3f;
const int Gmax = 75001;
int *d[2],*de;
int N,G,v[201];
int main(){
    in>>N>>G;
    d[0]=new int[G+1];
    d[1]=new int[G+1];
    de=new int[G+1];
    for(int i=1;i<=N;i++) in>>v[0],v[v[0]]++;
    int p=0; for(int j=0;j<=G;j++) d[p][j]=INF; d[p][0]=0;
    for(int i=1;i<=200;i++) if(v[i]){
        p=!p; for(int j=0;j<=G;j++) d[p][j]=INF;
        int x=i;
        for(int k=0;k<x;k++){
            int st=1,dr=0;
            for(int j=k;j<=G;j+=x){
                while(st<=dr && d[!p][j] < (j-de[dr])/x + d[!p][de[dr]]) dr--;
                while(st<=dr && (j-de[st])/x > v[x]) st++;
                d[p][j]=d[!p][j];
                if(st<=dr && (j-de[st])/x + d[!p][de[st]] < d[p][j]){
                    d[p][j]=(j-de[st])/x+d[!p][de[st]];
                }
                de[++dr]=j;
            }
        }
    }
    int f=G;
    for(int j=G;j>=0;j--){
        if(d[p][j]<INF){
            f=j;
            break;
        }
    }
    out<<f<<' '<<d[p][f]<<'\n';
    // enough
    return 0;
}