2015 CCPC D- Pick The Sticks(UESTC 1218) (01背包变形)

http://acm.uestc.edu.cn/#/problem/show/1218

既然二维dp表示不了,就加一维表示是否在边界放置,放置一个,两个。有一个trick就是如果只放一根,那么多长都可以。

wa了好多次(囧)

开始因为l[i]/2会出现小数,没注意,把所有的长度都x2就可以解决。

又wa了n次因为没注意j-l[i]时没加判断,为什么不是RE呢!不开心。。。

/*********************************************
Memory: 1140 KB		Time: 3976 MS
Language: C++		Result: Accepted
*********************************************/
#include <iostream>
#include <cstring>

using namespace std;

typedef long long ll;

int l[1005];
int v[1005];
ll dp[4005][5];

int main()
{
    int t;
    cin >> t;
    int cas = 0;
    while (t--)
    {
        cas++;
        int n, L;
        cin >> n >> L;
        L *= 2;
        ll ans = 0;
        for (int i = 0; i < n; ++i)
        {
            cin >> l[i] >> v[i];
            l[i] *= 2;
            ans = max(ans, (ll)v[i]);
        }
        memset(dp, 0, sizeof dp);
        for (int i = 0; i < n; ++i)
        {
            for (int j = L; j >= l[i] / 2; --j)
            {
                dp[j][2] = max(dp[j][2], dp[j - l[i] / 2][1] + v[i]);
                if (j >= l[i]) dp[j][2] = max(dp[j][2], dp[j - l[i]][2] + v[i]);
                dp[j][1] = max(dp[j][1], dp[j - l[i] / 2][0] + v[i]);
                if (j >= l[i]) dp[j][1] = max(dp[j][1], dp[j - l[i]][1] + v[i]);
                if (j >= l[i]) dp[j][0] = max(dp[j][0], dp[j - l[i]][0] + v[i]);
            }
        }
        ans = max(ans, dp[L][2]);
        cout << "Case #" << cas << ": " << ans << endl;
    }
    return 0;
}

/**
Input:
50
2 3
1 2
2 3

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

Output:
5
2
6
11
3

*/

  

时间: 2024-08-04 05:12:05

2015 CCPC D- Pick The Sticks(UESTC 1218) (01背包变形)的相关文章

2015南阳CCPC D - Pick The Sticks 背包DP.

D - Pick The Sticks Description The story happened long long ago. One day, Cao Cao made a special order called "Chicken Rib" to his army. No one got his point and all became very panic. However, Cao Cao himself felt very proud of his interesting

2015南阳CCPC D - Pick The Sticks dp

D - Pick The Sticks Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description The story happened long long ago. One day, Cao Cao made a special order called "Chicken Rib" to his army. No one got his point and all became very panic. However, Cao

ACM学习历程—UESTC 1218 Pick The Sticks(动态规划)(2015CCPC D)

题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 题目大意就是求n根木棒能不能放进一个容器里,乍一看像01背包,但是容器的两端可以溢出容器,只要两端的木棒的重心还在容器中即可. 首先由于木棒可以两端溢出.一端溢出和不溢出三种情况,所以有状态p(flag, v)表示溢出个数为flag的容量为v的情况下的最值. 于是有: p[2][j] = max(p[2][j], p[2][j-a[i]]+v[i]); p[2][j] = max(p[2][j], 

DP(01背包) UESTC 1218 Pick The Sticks (15CCPC C)

题目传送门 题意:长度为L的金条,将n根金棍尽可能放上去,要求重心在L上,使得价值最大,最多有两条可以长度折半的放上去. 分析:首先长度可能为奇数,先*2.然后除了两条特殊的金棍就是01背包,所以dp[now][j][k]表示当前状态,长度为j,使用了k条特殊金棍获得的最大价值,需要对内存和时间优化. /************************************************ * Author :Running_Time * Created Time :2015/10/2

hdu5543(Pick The Sticks) 01背包

Pick The Sticks Time Limit: 15000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 2540    Accepted Submission(s): 850 Problem Description The story happened long long ago. One day, Cao Cao made a special order

2015暑假多校联合---CRB and His Birthday(01背包)

题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5410 Problem Description Today is CRB's birthday. His mom decided to buy many presents for her lovely son.She went to the nearest shop with M Won(currency unit).At the shop, there are N kinds of pr

[2012山东ACM省赛] Pick apples (贪心,全然背包,枚举)

Pick apples Time Limit: 1000MS Memory limit: 165536K 题目描写叙述 Once ago, there is a mystery yard which only produces three kinds of apples. The number of each kind is infinite. A girl carrying a big bag comes into the yard. She is so surprised because s

【起航计划 002】2015 起航计划 Android APIDemo的魔鬼步伐 01

本文链接:[起航计划 002]2015 起航计划 Android APIDemo的魔鬼步伐 01 参考链接:http://blog.csdn.net/column/details/mapdigitapidemos.html?&page=12

HDU 5543 Pick The Sticks:01背包变种

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5543 题意: 给你N个金条和一张长度为L的桌子.每个金条长度为a[i],价值为w[i].金条只能在桌子上横着摆一排,并且只要金条的重心(中心)在桌子上,就可以放.问你在桌子上能够摆的金条的最大总价值. 题解: 首先表示状态: 考虑到第i个金条,在这之前已经占用了j的长度,在k个端点摆了金条.即:dp[i][j][k] 如何转移(逆推): 对于第i个金条,有三种决策:摆在桌子中央.摆在桌子端点处.不摆