2014
11-05

# AreYouBusy

Happy New Term!
As having become a junior, xiaoA recognizes that there is not much time for her to AC problems, because there are some other things for her to do, which makes her nearly mad.
What’s more, her boss tells her that for some sets of duties, she must choose at least one job to do, but for some sets of things, she can only choose at most one to do, which is meaningless to the boss. And for others, she can do of her will. We just define the things that she can choose as "jobs". A job takes time , and gives xiaoA some points of happiness (which means that she is always willing to do the jobs).So can you choose the best sets of them to give her the maximum points of happiness and also to be a good junior(which means that she should follow the boss’s advice)?

There are several test cases, each test case begins with two integers n and T (0<=n,T<=100) , n sets of jobs for you to choose and T minutes for her to do them. Follows are n sets of description, each of which starts with two integers m and s (0<m<=100), there are m jobs in this set , and the set type is s, (0 stands for the sets that should choose at least 1 job to do, 1 for the sets that should choose at most 1 , and 2 for the one you can choose freely).then m pairs of integers ci,gi follows (0<=ci,gi<=100), means the ith job cost ci minutes to finish and gi points of happiness can be gained by finishing it. One job can be done only once.

There are several test cases, each test case begins with two integers n and T (0<=n,T<=100) , n sets of jobs for you to choose and T minutes for her to do them. Follows are n sets of description, each of which starts with two integers m and s (0<m<=100), there are m jobs in this set , and the set type is s, (0 stands for the sets that should choose at least 1 job to do, 1 for the sets that should choose at most 1 , and 2 for the one you can choose freely).then m pairs of integers ci,gi follows (0<=ci,gi<=100), means the ith job cost ci minutes to finish and gi points of happiness can be gained by finishing it. One job can be done only once.

3 3
2 1
2 5
3 8
2 0
1 0
2 1
3 2
4 3
2 1
1 1

3 4
2 1
2 5
3 8
2 0
1 1
2 8
3 2
4 4
2 1
1 1

1 1
1 0
2 1

5 3
2 0
1 0
2 1
2 0
2 2
1 1
2 0
3 2
2 1
2 1
1 5
2 8
3 2
3 8
4 9
5 10

5
13
-1
-1

可以把每堆工作当做一个组，然后每个组有自己的状态转移方程;

当某组的性质为1时，就是分组背包，但这里是用二维数组，在一组计算完成之后，要把前一组的结果复制下来。

当某组的性质为2时，就把这组当成01背包来做，也要记得把前一组的结果复制下来，因为本组可以不选。

本题有个trick，那就是容量是从0开始的，和常规的容量从1开始不一样，要注意for循环里的下界。

1 0
2 0
0 1
0 2

1 0
2 2
0 1
0 2

1 1000
2 0
1 1
2 2

#include <stdio.h>
#include <string.h>
#define MAX 102
#define max(a,b) (a) > (b) ? (a) : (b)

int ans,dp[MAX][MAX];
int n,m,num[MAX],flag[MAX];
int cost[MAX][MAX],val[MAX][MAX];

int Solve_1A() {

int i,j,k,tpval;

dp[0][0] = 0;
for (i = 1; i <= n; ++i) {

if (flag[i] == 2) {
//01背包
for (k = 1; k <= num[i]; ++k)
for (j = m; j >= cost[i][k]; --j) {

tpval = dp[i][j-cost[i][k]];
if ( tpval != -1) dp[i][j] = max(dp[i][j],tpval+val[i][k]);
tpval = dp[i-1][j-cost[i][k]];
if ( tpval != -1) dp[i][j] = max(dp[i][j],tpval+val[i][k]);

}
for (j = 0; j <= m; ++j)
dp[i][j] = max(dp[i][j],dp[i-1][j]);
}
else if (flag[i] == 1) {
//分组背包，最多选一个,保证dp[i][j]只由上一次的一个状态转移而来
for (j = m; j >= 0; --j)
for (k = 1; k <= num[i]; ++k)
if (j >= cost[i][k]) {

tpval = dp[i-1][j-cost[i][k]];
if (tpval != -1) dp[i][j] = max(dp[i][j],tpval+val[i][k]);
}
for (j = 0; j <= m; ++j)
dp[i][j] = max(dp[i][j],dp[i-1][j]);
}
else {
//至少选一个的分组背包
for (k = 1; k <= num[i]; ++k)
for (j = m; j >= cost[i][k]; --j) {

tpval = dp[i][j-cost[i][k]];
if (tpval != -1)
dp[i][j] = max(dp[i][j],tpval+val[i][k]);
tpval = dp[i-1][j-cost[i][k]];
if (tpval != -1)
dp[i][j] = max(dp[i][j],tpval+val[i][k]);

}
}

//本组不合法，可直接返回-1
for (j = 0; j <= m; ++j)
if (dp[i][j] != -1) break;
if (j == m + 1) return -1;
}

for (ans = -1,i = 0; i <= m; ++i)
ans = max(ans,dp[n][i]);
return ans;
}

int main()
{
int i,j,k,t,tpval;

while (scanf("%d%d",&n,&m) != EOF) {

memset(dp,-1,sizeof(dp));
memset(num,0,sizeof(num));
for (i = 1; i <= n; ++i) {

scanf("%d%d",&num[i],&flag[i]);
for (j = 1; j <= num[i]; ++j)
scanf("%d%d",&cost[i][j],&val[i][j]);
}

int ans = Solve_1A();
printf("%d\n",ans);
}
}

1. 我没看懂题目
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
我觉得第一个应该是5 6 -1 5 4 输出是19 5 4
第二个是7 0 6 -1 1 -6 7输出是14 7 7
不知道题目例子是怎么得出来的

2. 我还有个问题想请教一下，就是感觉对于新手来说，递归理解起来有些困难，不知有没有什么好的方法或者什么好的建议？