hdu oj 3127 WHUgirls(2009 Asia Wuhan Regional Contest Online)
WHUgirls
Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2068 Accepted Submission(s): 785
style, and they voted each style a price wrote down on a list. They have a machine which can cut one cloth into exactly two smaller rectangular pieces horizontally or vertically, and ask you to use this machine to cut the original huge cloth into pieces appeared
in the list. Girls wish to get the highest profit from the small pieces after cutting, so you need to find out a best cutting strategy. You are free to make as many scarves of a given style as you wish, or none if desired. Of course, the girls do not require
you to use all the cloth.
The first line of each case consists of three integers N, X, Y, N indicating there are N kinds of rectangular that you can cut in and made to scarves; X, Y indicating the dimension of the original cloth. The next N lines, each line consists of two integers,
xi, yi, ci, indicating the dimension and the price of the ith rectangular piece cloth you can cut in.
Constrains
0 < T <= 20
0 <= N <= 10; 0 < X, Y <= 1000
0 < xi <= X; 0 < yi <= Y; 0 <= ci <= 1000
1
2 4 4
2 2 2
3 3 9
9
这个题開始做的时候。一直分析。感觉应该是要用动态规划来做。其它做法应该会要超时,然后一直往动态规划那方面想。结果越想越复杂,感觉情况太多了。考虑不周全,后来看了一下别人的思路,直接能够转化为二维的全然背包问题。一大块完整的布就相当于一个背包,剪布料就相当于往里面放东西,由于剪矩形有长和宽。所以就是二维来做,每剪一块的价值就相当于背包的价值,最关键的是每一种能够剪多次,全然背包里面的一种物品能够放多次;
看了这一篇博客的分析:http://blog.csdn.net/lulipeng_cpp/article/details/7587465
题目的意思就是要我们能够剪的最大价值,所以设立 dp[i][j]:长为i宽为j的矩形布的最大价值;
由于剪成矩形有两种情况能够剪,(能够參看上面的博客),一种直接是依照长来剪,还能够依照宽来剪。剪了之后,一块矩形的布可能就不是一个矩形了,我们就把它分成三块来算,分成3个小矩形。我们要求的就是这三个小矩形的面积之和;设立 要剪去的矩形的长为x,宽为y。(图片直接是上面的博客里面的)
我们就有两种剪法:
第一种:
这里我们就能够得到方法一的状态方程式;w为剪去的小矩形的价值;
dp[i][j]=max(dp[i][j],max(dp[i-x][j]+dp[x][j-y],dp[i][j-y]+dp[i-x][y])+w);
另外一种:
dp[i][j]=max(dp[i][j],max(dp[i-y][j]+dp[y][j-x],dp[i][j-x]+dp[i-y][x])+w);
以下的ac的代码。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct bag
{
int x,y,value;
};
bag bag[10];
int dp[1001][1001];
int main()
{
int t,n,x,y,i,j,k;
scanf("%d",&t);
while(t--)
{
memset(dp,0,sizeof(dp));
scanf("%d%d%d",&n,&x,&y);
for(i=0;i<n;i++)
scanf("%d%d%d",&bag[i].x,&bag[i].y,&bag[i].value);
for(i=0;i<=x;i++)
for(j=0;j<=y;j++)
for(k=0;k<n;k++)
{
if(i>=bag[k].x && j>=bag[k].y)//第一种剪法;
dp[i][j]=max(dp[i][j],max((dp[i-bag[k].x][j]+dp[bag[k].x][j-bag[k].y]),(dp[i][j-bag[k].y]+dp[i-bag[k].x][bag[k].y]))+bag[k].value);
if(i>=bag[k].y && j>=bag[k].x)//另外一种剪法;
dp[i][j]=max(dp[i][j],max((dp[i-bag[k].y][j]+dp[bag[k].y][j-bag[k].x]),(dp[i][j-bag[k].x]+dp[i-bag[k].y][bag[k].x]))+bag[k].value);
}
printf("%d\n",dp[x][y]);
}
return 0;
}
hdu oj 3127 WHUgirls(2009 Asia Wuhan Regional Contest Online)的更多相关文章
- hdu 3123 GCC (2009 Asia Wuhan Regional Contest Online)
GCC Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Subm ...
- HDU 3126 Nova [2009 Asia Wuhan Regional Contest Online]
标题效果 有着n巫妖.m精灵.k木.他们都有自己的位置坐标表示.冷却时间,树有覆盖范围. 假设某个巫妖攻击精灵的路线(他俩之间的连线)经过树的覆盖范围,表示精灵被树挡住巫妖攻击不到.求巫妖杀死所有精灵 ...
- hdu 3123 2009 Asia Wuhan Regional Contest Online
以为有啥牛逼定理,没推出来,随便写写就A了----题非常水,可是wa了一次 n>=m 则n!==0 注意的一点,最后 看我的凝视 #include <cstdio> #includ ...
- hdu3231 (三重拓扑排序) 2009 Asia Wuhan Regional Contest Hosted by Wuhan University
这道题算是我拓扑排序入门的收棺题了,卡了我好几天,期间分别犯了超时,内存溢出,理解WA,细节WA,格式WA…… 题目的意思大概是在一个三维坐标系中,有一大堆矩形,这些矩形的每条棱都与坐标轴平行. 这些 ...
- HDU 3269 P2P File Sharing System(模拟)(2009 Asia Ningbo Regional Contest)
Problem Description Peer-to-peer(P2P) computing technology has been widely used on the Internet to e ...
- HDU 3721 Building Roads (2010 Asia Tianjin Regional Contest) - from lanshui_Yang
感慨一下,区域赛的题目果然很费脑啊!!不过确实是一道不可多得的好题目!! 题目大意:给你一棵有n个节点的树,让你移动树中一条边的位置,即将这条边连接到任意两个顶点(边的大小不变),要求使得到的新树的直 ...
- HDU 5074 Hatsune Miku 2014 Asia AnShan Regional Contest dp(水
简单dp #include <stdio.h> #include <cstring> #include <iostream> #include <map> ...
- zoj 3659 Conquer a New Region The 2012 ACM-ICPC Asia Changchun Regional Contest
Conquer a New Region Time Limit: 5 Seconds Memory Limit: 32768 KB The wheel of the history roll ...
- 2018 ACM-ICPC Asia Beijing Regional Contest (部分题解)
摘要 本文主要给出了2018 ACM-ICPC Asia Beijing Regional Contest的部分题解,意即熟悉区域赛题型,保持比赛感觉. Jin Yong’s Wukong Ranki ...
随机推荐
- druid监控及慢sql记录
本文提要 前文也提到过druid不仅仅是一个连接池技术,因此在将整合druid到项目中后,这一篇文章将去介绍druid的其他特性和功能,作为一个辅助工具帮助提升项目的性能,本文的重点就是两个字:监控. ...
- python游戏开发:pygame中的IO、数据
一.python输入输出 1.输出 python一次可以打印多个变量,只要用一个逗号将每个变量隔开就可以了.比如: A = 123B = "ABC"C = 456D = " ...
- JS Object 属性判断
in 方法 var shapeInfo = {name:“lium”}; if (“name” in shapeInfo) {...}
- MSYS2 使用
在Windows下编译mongo-c-driver 1.3.x 在Windows下编译mongo-c-driver 1.3.x 1.安装 MSYS2https://sourceforge.net/pr ...
- 数据库insert update select语句
http://database.51cto.com/art/200903/113939_1.htm (更新语句) http://blog.csdn.net/chan ...
- Java中9大内置基本数据类型Class实例和数组的Class实例(转载)
https://www.jianshu.com/p/58976c8bf1e1
- Linux环境下c程序的编译和执行
1 单个文件的编译和执行创建main.c文件,内容如下: #include <stdio.h> #include <stdlib.h> int main(void){ prin ...
- Kvm:通过 libvirt 远程管理虚拟机
1.通过qemu+ssh方式 2.通过qemu+tcp方式 主控端需要安装相关工具包: #yum groupinstall "Virtualization" #yum instal ...
- pop(),del A[:], a[:] = b[:]/'str'/可迭代的
s = ['a','ma','shi','ge'] s0 = s.pop(0) #---> 有返回值 print(s,s0) s1 = s.remove('shi') #---> 无返回值 ...
- 第十六节:Scrapy爬虫框架之项目创建spider文件数据爬取
Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取所设计的, 也可以应用在获取API所返回的数据或 ...