题目:

思路:

dp[j][h]表示选取了j个向量,且高度为h,利用01背包来解决问题。

没选当前的向量:dp[j][h] = dp[j][h];

选了当前的向量:dp[j][h] = dp[j-1][h-p[i].y]+2*p[i].x*(h-p[i].y)+p[i].area;(p[i].area = p[i].x+p[i].y)

代码:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define MAX 1000000000
#define mod 1000000007
#define FRE() freopen("in.txt","r",stdin)
#define FRO() freopen("out.txt","w",stdout)
using namespace std;
typedef long long ll;
typedef pair<int,ll> pii;
const int maxn = ;
int dp[maxn][maxn*maxn];
int n,k,H;
struct Point{
int x,y,area;
}p[maxn]; bool cmd(const Point& a,const Point& b){
return a.y*b.x > a.x*b.y;
} int main(){//01背包
//FRE();
int kase;
scanf("%d",&kase);
for(int o=; o<=kase; o++){
H = ;
scanf("%d%d",&n,&k);
for(int i=; i<=n; i++){
scanf("%d%d",&p[i].x,&p[i].y);
p[i].area = p[i].x*p[i].y;
H += p[i].y;
}
sort(p+,p+n+,cmd);
memset(dp,-,sizeof(dp));
dp[][] = ;
for(int i=; i<=n; i++){//枚举所有的向量
for(int j=k; j>=; j--){//枚举到向量i时,对k个向量中的值进行更新
for(int h=H; h>=p[i].y; h--){
if(dp[j-][h-p[i].y]!=-){
dp[j][h] = max(dp[j][h],dp[j-][h-p[i].y]+(h-p[i].y)*p[i].x*+p[i].area);
}
}
}
}
int ans=;
for(int i=; i<=H; i++){
ans = max(ans,dp[k][i]);
}
printf("Case %d: %d\n",o,ans);
}
return ;
}

UVA - 12589 Learning Vector(dp-01背包)的更多相关文章

  1. UVA 562 Dividing coins(dp + 01背包)

    Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were figh ...

  2. uva 12589 - Learning Vector

    思路: 容易知道加向量的顺序是按向量斜率的大小顺序来的.由于数据不是很大,可以用背包解决!! dp[i][j]:加入最大面积为i时,加入了j个向量. 代码如下: #include<iostrea ...

  3. UVA.10130 SuperSale (DP 01背包)

    UVA.10130 SuperSale (DP 01背包) 题意分析 现在有一家人去超市购物.每个人都有所能携带的重量上限.超市中的每个商品有其相应的价值和重量,并且有规定,每人每种商品最多购买一个. ...

  4. USACO Money Systems Dp 01背包

    一道经典的Dp..01背包 定义dp[i] 为需要构造的数字为i 的所有方法数 一开始的时候是这么想的 for(i = 1; i <= N; ++i){ for(j = 1; j <= V ...

  5. HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

  6. POJ.3624 Charm Bracelet(DP 01背包)

    POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...

  7. HDOJ(HDU).2546 饭卡(DP 01背包)

    HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...

  8. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

  9. UVA 624 CD(DP + 01背包)

    CD You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music i ...

随机推荐

  1. HDU 5976 Detachment 【贪心】 (2016ACM/ICPC亚洲区大连站)

    Detachment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  2. 4.7.3 Canonical LR(1) Parsing Tables

    4.7.3 Canonical LR(1) Parsing Tables We now give the rules for constructing the LR(1) ACTION and GOT ...

  3. POJ 3294 UVA 11107 Life Forms 后缀数组

    相同的题目,输出格式有区别. 给定n个字符串,求最长的子串,使得它同时出现在一半以上的串中. 不熟悉后缀数组的童鞋建议先去看一看如何用后缀数组计算两个字符串的最长公共子串 Ural1517 这道题的思 ...

  4. bzoj1604

    treap+并查集 我们能想到一个点和最近点对连接,用并查集维护,但是这个不仅不能求,而且还是不对的,于是就看了题解 把距离转为A(x-y,x+y),这样两点之间的距离就是max(x'-X',y'-Y ...

  5. uva1560

    In an extended version of the game Lights Out®, is a puzzle with 5 rows of 6 buttons each (the actua ...

  6. SSM整合配置错误记录

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dic ...

  7. PCB 2019年IT工作主题【降本增效】 词云

    降本增效是IT部门永恒的主题,从自身做起.踏踏实实把工作做好 在线词云制作软件: https://wordart.com/create

  8. 使用docsify并定制以使它更强大

    背景 经常在网上看到一些排版非常漂亮的技术手册,左边有目录栏,右边是Markdown格式的文档,整个配色都十分舒服,就像一本书一样,一看就很让人喜欢.就比如Markdown Preview Enhan ...

  9. Akka源码分析-Akka-Streams-GraphStage

    上一篇博客中我们介绍了ActorMaterializer的一小部分源码,其实分析的还是非常简单的,只是初窥了Materializer最基本的初始化过程及其涉及的基本概念.我们知道在materializ ...

  10. 虚拟机安装cenos7后ifcfg看网卡无inet地址掩码等信息

    在虚拟机安装centos7,进入系统使用ifconfig命令时,只有lo网卡( 127.0.0.1的ip地址)和eno16777736网卡,而且此网卡没有inet地址.掩码等信息. 这时候查看/etc ...