Description
Xiu Yang, one of the cleverest counselors of Cao Cao, understood the command Rather than keep it to himself, he told the point to the whole army. Cao Cao got very angry at his cleverness and would like to punish Xiu Yang. But how can you punish someone because he's clever? By looking at the chicken rib, he finally got a new idea to punish Xiu Yang.
He told Xiu Yang that as his reward of encrypting the special order, he could take as many gold sticks as possible from his desk. But he could only use one stick as the container.
Formally, we can treat the container stick as an
length segment. And the gold sticks as segments too. There were many gold sticks with different length

and value

. Xiu Yang needed to put these gold segments onto the container segment. No gold segment was allowed to be overlapped. Luckily, Xiu Yang came up with a good idea. On the two sides of the container, he could make part of the gold sticks outside the container as long as the center of the gravity of each gold stick was still within the container. This could help him get more valuable gold sticks.
As a result, Xiu Yang took too many gold sticks which made Cao Cao much more angry. Cao Cao killed Xiu Yang before he made himself home. So no one knows how many gold sticks Xiu Yang made it in the container.
Can you help solve the mystery by finding out what's the maximum value of the gold sticks Xiu Yang could have taken?
Input









.
test cases follow. Each test case start with two integers,










and










, represents the number of gold sticks and the length of the container stick.
lines follow. Each line consist of two integers,












and











, represents the length and the value of the


gold stick.
Output
is the test case number (starting from 1) and
is the maximum value of the gold sticks Xiu Yang could have taken.
Sample Input
4
3 7
4 1
2 1
8 1
3 7
4 2
2 1
8 4
3 5
4 1
2 2
8 9
1 1
10 3
Sample Output
Case #1: 2
Case #2: 6
Case #3: 11
Case #4: 3
Hint
In the third case, assume the container is lay on x-axis from 0 to 5. Xiu Yang could put the second gold stick center at 0 and put the third gold stick center at 5, so none of them will drop and he can get total 2+9=11 value. In the fourth case, Xiu Yang could just put the only gold stick center on any position of [0,1], and he can get the value of 3.
还有另外一个问题就是,只把重心放在容器里面把体积的一半放进去,这是会出现小数的,所以我们应该把全部体积乘上2再计算。下面给代码。
#include<iostream>
#include<stack>
#include<cstring>
#include<map>
#include<string>
#include<queue>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
int main(){
int t;
scanf("%d", &t);
for (int tcase = 1; tcase <= t;tcase++){
int n, l;
scanf("%d%d", &n, &l);
int a[1005], b[1005];
LL dp[4010][2][3];
LL maxnum = 0;
for (int i = 0; i < n; i++){
scanf("%d%d", &a[i], &b[i]);
a[i] =a[i]<<1;
maxnum = max((LL)b[i], maxnum);
}
memset(dp, 0, sizeof(dp));
int now = 1;
for (int i = 0; i < n; i++){
now = now ^ 1;
for (int j = 1; j <= l<<1; j++){
for (int k = 0; k < 3; k++){
dp[j][now][k] = dp[j][now ^ 1][k];
if (j - a[i] >= 0){
dp[j][now][k] = max(dp[j][now][k], dp[j - a[i]][now ^ 1][k]+b[i]);
}
if (k&&(j - (a[i]>>1)) >= 0){
dp[j][now][k] = max(dp[j][now][k], dp[j - (a[i] >>1)][now ^ 1][k-1]+b[i]);
}
}
}
}
maxnum = max(maxnum, dp[l<<1][now][0]);
maxnum = max(maxnum, dp[l<<1][now][1]);
maxnum = max(maxnum, dp[l<<1][now][2]);
printf("Case #%d: %lld\n",tcase, maxnum);
}
}
思路二:
用dp[i][M][k] 表示用前i种物品放在长度为M的大木块上,两端放了k个小木棒的做法。
所以dp[i][j][k]=max dp[i-1][j-a[i].length][k] + a[i].value
dp[i][j][k]=max dp[i-1][j-a[i].length/2][k-1] + a[i].value
但是这样写会爆内存,那就降维吧。方法跟二维01背包降维一样
代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1005;
typedef long long LL;
LL dp[4*maxn][4],maxvalue;
int T,N,M;
struct node
{
LL value,length;
}a[maxn];
void init()
{
memset(dp,0,sizeof(dp));
}
void input()//输入
{
scanf("%d%d",&N,&M);
maxvalue=0;
M*=2;
for(int i=1;i<=N;i++)
{
scanf("%lld%lld",&a[i].length,&a[i].value);
a[i].length=a[i].length*2;
maxvalue=max(maxvalue,a[i].value);
}
}
int main()
{
scanf("%d",&T);
for(int tcase=1;tcase<=T;tcase++)
{
init();
input();
for(int i=1;i<=N;i++)//dp[i][j] 把前i件物品放入容量为j的背包的最大收益
{
for(int j=M;j>=a[i].length/2;j--)
{
for(int k=0;k<3;k++)
{
if(j-a[i].length>=0)dp[j][k]=max(dp[j][k],dp[j-a[i].length][k]+a[i].value);
if(k>0)
{
if(j-a[i].length/2>=0)dp[j][k]=max(dp[j][k],dp[j-a[i].length/2][k-1]+a[i].value);
}
}
}
}
printf("Case #%d: ",tcase);
LL ans=max(max(dp[M][0],dp[M][1]),max(dp[M][2],maxvalue));
cout<<ans<<endl;
}
}
797



被折叠的 条评论
为什么被折叠?



