Cod sursa(job #887297)

Utilizator veleanduAlex Velea veleandu Data 23 februarie 2013 17:51:53
Problema Ghiozdan Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb
#include<cstdio>

#include <algorithm>

using namespace std;

#define max_g 75005
#define fi first
#define se second
#define th third

struct trio {
	int first,second,third;
	trio(){
		first=second=third=0;
	}
	trio( int a, int b, int c ){
 		first=a;
		second=b;
		third=c;
	}
} Deq[ max_g ];

int Ap[ 205 ],n,g,i,a;
int st,dr, Min[ max_g ];

void read( ){
	freopen("ghiozdan.in","r",stdin);
	freopen("ghiozdan.out","w",stdout);
	scanf("%d %d", &n, &g );
	for ( i=1; i<=n; ++i ){
		scanf("%d", &a );
		++Ap[ a ];
	}
}
void getmin( int &a, int b ){
	if( b < a )
		a=b;
	if( a == 0 )
		a=b;
}

void solve( int c1, int c2, int g ){
	// scriem suma g din valorile de la st la dr inclusiv
    int d,r,st,dr,ind,step;

	Min[ 0 ] = 1;
	for( d=c1; d<=c2; ++d ){
 		if( Ap[ d ] ){
			for( r=0; r<d; ++r ){
				st=dr=1;
				dr=0;
				ind=r+d;
				step=1;
				if( Min[ r ] )
					Deq[ ++dr ]=trio( r, 0, Min[ r ] );
				for( ; ind<=g; ind+=d,++step ){
					if( Deq[ st ].se + Ap[ d ] < step ){
						st++;
					}
					if( Min[ ind ] ){
						// scoatem din stiva cat putem.
						while( st<=dr && Min[ ind ] <= Min[ Deq[ dr ].fi ] + ( step - Deq[ dr ].se ) )
							--dr;
						Deq[ ++dr ] = trio( ind, step, Min[ ind ] );
					}
					if( st <= dr )
						getmin( Min[ ind ], step - Deq[ st ].se + Deq[ st ].th );
				}
			}
		}
	}
	for( i=g; i; --i ){
		if( Min[ i ] ){
			printf("%d %d\n",i,Min[i]-1);
			return ;
		}
	}
}

int main(){
	read();
	solve( 1, 200, g );
	return 0;
}