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/21 星期三 11:55:40
 * File Name     :D.cpp
 ************************************************/
#include <bits/stdc++.h>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e3 + 10;
const int L = 4e3 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
ll dp[2][L][3];
struct P    {
    int a;
    ll v;
}p[N];

ll Max(ll a, ll b)    {
    return a > b ? a : b;
}

int main(void)    {
    int T, cas = 0; scanf ("%d", &T);
    while (T--) {
        int n, l;   scanf ("%d%d", &n, &l);
        l *= 2;
        ll ans = 0;
        for (int i=1; i<=n; ++i)    {
            scanf ("%d%lld", &p[i].a, &p[i].v);
            p[i].a *= 2;
            ans = max (ans, p[i].v);
        }
        memset (dp, 0, sizeof (dp));
        int now = 1;
        for (int i=1; i<=n; ++i)    {
            now = 1 - now;
            for (int j=0; j<=l; ++j)   {
                for (int k=0; k<3; ++k) {
                    dp[now][j][k] = dp[1-now][j][k];
                }
            }
            for (int j=l; j>=p[i].a/2; --j) {
                for (int k=0; k<3; ++k) {
                    if (j >= p[i].a)    dp[now][j][k] = max (dp[now][j][k], dp[1-now][j-p[i].a][k] + p[i].v);
                    if (k)  dp[now][j][k] = max (dp[now][j][k], dp[1-now][j-p[i].a/2][k-1] + p[i].v);
                }
            }
            for (int i=0; i<2; ++i) {
                for (int j=0; j<=l; ++j) {
                    for (int k=0; k<3; ++k) ans = max (ans, dp[i][j][k]);
                }
            }
        }
        printf ("Case #%d: %lld\n", ++cas, ans);
    }

    return 0;
}

  

时间: 2024-12-22 19:15:11

DP(01背包) UESTC 1218 Pick The Sticks (15CCPC C)的相关文章

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], 

USACO Money Systems Dp 01背包

一道经典的Dp..01背包 定义dp[i] 为需要构造的数字为i 的所有方法数 一开始的时候是这么想的 for(i = 1; i <= N; ++i){ for(j = 1; j <= V; ++j){ if(i - a[j] > 0){ dp[i] += dp[i - a[j]]; } } } 状态存在冗余, 输出的时候答案肯定不对 但只需要改一下两个for循环的顺序即可. Source Code: /* ID: wushuai2 PROG: money LANG: C++ */ //

hdu 1561The more, The Better(树形dp&amp;01背包)

The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4949    Accepted Submission(s): 2918 Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝

poj 3345 Bribing FIPA 【树形dp + 01背包】

Bribing FIPA Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4274   Accepted: 1337 Description There is going to be a voting at FIPA (Fédération Internationale de Programmation Association) to determine the host of the next IPWC (Interna

poj 1837 Balance (dp,01背包)

链接:poj 1837 题意:有一个天平,天平左右两边各有若干个钩子,总共有C个钩子,有G个钩码, 求将钩码挂到钩子上使天平平衡的方法的总数.其中可以把天枰看做一个以x轴0点作为平衡点的横轴 分析:力臂=重量 *臂长 = g[i]*c[j] 当平衡度k=0时,说明天枰达到平衡,k>0,说明天枰倾向右边(x轴右半轴),k<0则左倾 因此可以定义一个 状态数组dp[i][k],意为在挂满前i个钩码时,平衡度为k的挂法的数量. 由于距离c[i]的范围是-15~15,钩码重量的范围是1~25,钩码数量

hdu 4044 GeoDefense (树形dp+01背包)

GeoDefense Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 663    Accepted Submission(s): 267 Problem Description Tower defense is a kind of real-time strategy computer games. The goal of towe

Bone Collector(dp 01背包)

Problem Description Many years ago , in Teddy's hometown there was a man who was called "Bone Collector". This man like to collect varies of bones , such as dog's , cow's , also he went to the grave - The bone collector had a big bag with a volu

HDU 3033 I love sneakers! (DP 01背包+完全背包)

Problem Description After months of hard working, Iserlohn finally wins awesome amount of scholarship. As a great zealot of sneakers, he decides to spend all his money on them in a sneaker store. There are several brands of sneakers that Iserlohn wan

poj 2923 状压dp+01背包

好牛b的思路 题意:一系列物品,用二辆车运送,求运送完所需的最小次数,两辆车必须一起走 解法为状态压缩DP+背包,本题的解题思路是先枚举选择若干个时的状态,总状态量为1<<n,判断这些状态集合里的那些物品能否一次就运走,如果能运走,那就把这个状态看成一个物品.预处理完能从枚举中找到tot个物品,再用这tol个物品中没有交集(也就是两个状态不能同时含有一个物品)的物品进行01背包,每个物品的体积是state[i],价值是1,求包含n个物品的最少价值也就是dp[(1<<n)-1](dp