Pagini recente » Cod sursa (job #1206364) | Cod sursa (job #548538) | Cod sursa (job #2700906) | Cod sursa (job #1432832) | Cod sursa (job #1737724)
#include <cstdio>
struct pct{
int a;
int b;
};
struct sol{
int i;
int a;
int b;
};
sol U[210][210];
sol USol[210][210];
pct P[210];
int V[210];
int W[210][210];
int WSol[210][210];
int Tata[210][210];
int TataSol[210][210];
pct S[210];
int p,u,N,L,T,i,j,k,poate,a,b,ni,nv,x,t,t1,t2,iSol,xSol;
int main(){
FILE *f = fopen("lapte.in","r");
fscanf(f,"%d %d", &N, &L);
for (i=1;i<=N;i++) {
fscanf(f,"%d %d",&P[i].a, &P[i].b);
}
fclose(f);
p=1;
u=101;
while (p<=u) {
T = (p+u)/2;
for (i=0;i<=200;i++) {
W[0][i] = W[1][i] = -1;
}
poate = 0;
for (i=1;i<=N;i++) {
for (j=0;j<=2*L;j++)
W[i][j] = -1;
for (j=0;j*P[i].a<=T;j++) {
a = j;
b = (T-j*P[i].a)/P[i].b;
if (W[i][a]<b) { //W[i][a] = nr maxim de litri de tip b bauti stiind ca s-au baut i litri de tip a
W[i][a] = b; //cu concurenti de la 1 la i
Tata[i][a] = 0;
U[i][a].a = a; //
U[i][a].b = b;
}
if (a>=L && b>=L) {
poate = 1;
xSol = a;
iSol = i;
for (t1=1;t1<=N;t1++)
for (t2=0;t2<=2*L;t2++){
WSol[t1][t2] = W[t1][t2];
TataSol[t1][t2] = Tata[t1][t2];
USol[t1][t2] = U[t1][t2];
}
break;
}
for (k=0;k<=L;k++)
if (W[i-1][k]!=-1) {
ni = k+a;
nv = W[i-1][k]+b;
if (W[i][ni]<nv){
W[i][ni] = nv;
Tata[i][ni] = k;
U[i][ni].a = a;
U[i][ni].b = b;
}
if (ni>=L && nv>=L) {
iSol = i;
xSol = ni;
for (t1=1;t1<=N;t1++)
for (t2=0;t2<=2*L;t2++){
WSol[t1][t2] = W[t1][t2];
TataSol[t1][t2] = Tata[t1][t2];
USol[t1][t2] = U[t1][t2];
}
poate = 1;
break;
}
}
if (poate)
break;
}
if (poate)
break;
}
if (poate) {
u = T-1;
} else {
p = T+1;
}
}
for (i=iSol;i>0;i--) {
S[i].a = USol[i][xSol].a;
S[i].b = USol[i][xSol].b;
xSol = TataSol[i][xSol];
}
FILE *g = fopen("lapte.out","w");
fprintf(g,"%d\n",p);
for (i=1;i<=N;i++)
fprintf(g,"%d %d\n",S[i].a,S[i].b);
fclose(g);
return 0;
}