题目:

思路:

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. Python获得文件时间戳 异常访问监控 邮件定时提醒

    Python获得文件时间戳  异常访问监控 邮件定时提醒

  2. 如何配置MYSQL的MASTER---SLAVE复制备份?

    如何配置MYSQL的MASTER---SLAVE复制备份? 一.配置一个mysql服务器做master:     在配置文件my.ini中添加如下内容: log-bin=matster-binlog- ...

  3. Vijos 1144 小胖守皇宫 【树形DP】

    小胖守皇宫 描述 huyichen世子事件后,xuzhenyi成了皇上特聘的御前一品侍卫. 皇宫以午门为起点,直到后宫嫔妃们的寝宫,呈一棵树的形状:某些宫殿间可以互相望见.大内保卫森严,三步一岗,五步 ...

  4. 167. Two Sum II - Input array is sorted (二分ortwo-pointer)

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  5. C语言8大经典排序算法(2)

    二.插入类排序 插入排序(Insertion Sort)的基本思想是:每次将一个待排序的记录,按其关键字大小插入到前面已经排好序的子文件中的适当位置,直到全部记录插入完成为止. 插入排序一般意义上有两 ...

  6. python-----自动解压并删除zip文件

    如何自动解压并删除zip? 如何解压  →  使用内置模块来实现(shutil.unpack_archive) 如何删除zip  →  使用内置模块os来实现(os.remove) 如何监测zip的出 ...

  7. NestedPreb

    屌丝手动版 One of the things we’re sorely missing from Unity is nested prefabs. So we rolled this little ...

  8. Authorization 头信息为空的解决方案

    在 Apache配置添加SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=此次问题完美解决.

  9. swoole之 swoole_process 应用于TP框架

    swoole_process 实现了多进程的管理,多个进程同时进行采集任务, 公司的框架比较low,用的tp框架,结合tp框架实现多进程的采集 这是swoole好的学习资源 https://segme ...

  10. SQL 理论知识总结

    1..如何设计数据库 答:存储信息的大小,每次扩容的大小,冗余 2.SQL Server的两种索引是何形式?索引的作用?索引的优缺点? 答:集聚索引,非聚集索引.提高查询速度.但是会过多的占用磁盘空间 ...