SGU 104 Little shop of flowers

SGU_104

    我们可以用f[i][j]、p[i][j]分别表示第i行扫描第j个瓶子时的最大值以及取得这个最大值时第i行的花放到了哪个瓶子中。

#include<stdio.h>
#include<string.h>
#define MAXD 110
#define INF 0x3fffffff
int F, V, f[MAXD][MAXD], v[MAXD][MAXD], p[MAXD][MAXD];
void printpath(int i, int j)
{
if(i != 1)
{
printpath(i - 1, p[i][j] - 1);
printf(" %d", p[i][j]);
}
else
printf("%d", p[i][j]);
}
void solve()
{
int i, j, k;
for(i = 1; i <= F; i ++)
for(j = 1; j <= V; j ++)
scanf("%d", &v[i][j]);
memset(f, 0xc1, sizeof(f));
for(i = 1; i <= V; i ++)
{
f[1][i] = f[1][i - 1];
p[1][i] = p[1][i - 1];
if(v[1][i] > f[1][i])
{
f[1][i] = v[1][i];
p[1][i] = i;
}
}
for(i = 2; i <= F; i ++)
for(j = i; j <= V; j ++)
{
f[i][j] = f[i][j - 1];
p[i][j] = p[i][j - 1];
if(v[i][j] + f[i - 1][j - 1] > f[i][j])
{
f[i][j] = v[i][j] + f[i - 1][j - 1];
p[i][j] = j;
}
}
printf("%d\n", f[F][V]);
printpath(F, V);
printf("\n");
}
int main()
{
while(scanf("%d%d", &F, &V) == 2)
{
solve();
}
return 0;
}


你可能感兴趣的:(it)