UVA - 12589 Learning Vector(dp-01背包)
题目:


思路:
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背包)的更多相关文章
- UVA 562 Dividing coins(dp + 01背包)
Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were figh ...
- uva 12589 - Learning Vector
思路: 容易知道加向量的顺序是按向量斜率的大小顺序来的.由于数据不是很大,可以用背包解决!! dp[i][j]:加入最大面积为i时,加入了j个向量. 代码如下: #include<iostrea ...
- UVA.10130 SuperSale (DP 01背包)
UVA.10130 SuperSale (DP 01背包) 题意分析 现在有一家人去超市购物.每个人都有所能携带的重量上限.超市中的每个商品有其相应的价值和重量,并且有规定,每人每种商品最多购买一个. ...
- USACO Money Systems Dp 01背包
一道经典的Dp..01背包 定义dp[i] 为需要构造的数字为i 的所有方法数 一开始的时候是这么想的 for(i = 1; i <= N; ++i){ for(j = 1; j <= V ...
- HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)
HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...
- POJ.3624 Charm Bracelet(DP 01背包)
POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...
- HDOJ(HDU).2546 饭卡(DP 01背包)
HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...
- HDOJ(HDU).2602 Bone Collector (DP 01背包)
HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...
- 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 ...
随机推荐
- python安装了2.7之后终端无法使用退格,上下左右
遇到RT问题,原因是由于在编译python的时候去烧readline库造成的 解决办法: yum install readline-devel 然后重新编译安装python,终端控制符可用!
- Codeforces Round #363 (Div. 2)E. LRU
E. LRU time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...
- 第十一周 Leetcode 576. Out of Boundary Paths (HARD) 计数dp
Leetcode 576 给定一个二维平面, 一个球在初始位置(i,j)每次可以转移到上下左右的一格. 问在N次转移内,有多少种路径可以转移出边境. dp[i][j][k]为 在点(i,j) 已经走了 ...
- libpcap 中调用ctime()时警告提示:
warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=] ...
- UEditor动态添加图片访问路径前缀
在使用UEditor上传图片时发现上传图片后在编辑器中不能显示上传的图片,在这里是需要在jsp/config.json中设置图片访问路径前缀,即项目的根路径,在config.json只能填写字符串的配 ...
- bzoj 1639: [Usaco2007 Mar]Monthly Expense 月度开支【二分】
忘开long long了居然没WA 二分答案,枚举判断看最后需要的月份数是否小于等于要求的即可 #include<iostream> #include<cstdio> usin ...
- Springboot 三种拦截Rest API的方法-过滤器、拦截器、切片
过滤器方式实现拦截(Filter) 通过继承Servlet的Filter类来实现拦截: @Component public class TimeFilter implements Filter { @ ...
- ACM_括号匹配
括号匹配(栈) Time Limit: 2000/1000ms (Java/Others) Problem Description: 给一组包含[]()两种括号的序列,检查是否是合法的. 如:()[] ...
- SQL优化器简介
文章导读: 什么是RBO? 什么是CBO? 我们在工作中经常会听到这样的声音:"SQL查询慢?你给数据库加个索引啊".虽然加索引并不一定能解决问题,但是这初步的体现了SQL优化的思 ...
- Spring(二) -- 春风拂面之 核心 AOP
”万物皆对象“是面向对象编程思想OOP(Object Oriented Programming) 的最高境界.在面向对象中,我一直将自己(开发者)放在一个至高无上的位置上,可以操纵万物(对象),犹如一 ...