NOJ 1116 哈罗哈的大披萨 【淡蓝】 [状压dp+各种优化]
我只能说,珍爱生命,远离卡常数的题。。。感谢陈老师和蔡神,没有他们,,,我调一个星期都弄不出来,,,,
哈罗哈的大披萨 【淡蓝】
总提交 : 73 测试通过 : 9
描述
热风哈罗哈(三牌楼)店正在搞活动:他们将提供一个大披萨给第一个告诉他们以下信息的人:一次购买任一种披萨,哪种单位面积的价格最低。问题初步想一想,好像是这么解决:“对于每个披萨计算平均价格,那么最小值就是答案了。”
但是,这个问题不是这么简单,因为在热风哈罗哈店里:对某些披萨,是送打折优惠券的,凭这些优惠券可以得到另一个便宜些甚至差点的披萨,这些优惠券可合并使用。披萨必须一个接一个的买,不可能使用优惠券对一个已买的披萨打折。你能第一个解决这个问题,得到大披萨吗?
输入
输入文件包含几个测试用例。每个测试用例包括:
l 1行:一个数m,为热风哈罗哈提供披萨数目。当m=0时输入终止。一般1 ≤ m ≤ 15。
l 随后的m 行描述每个披萨。每一行描述披萨i (1 ≤ i ≤ m) ,开始3个整数pi, ai 和 ni 表示披萨的价格、面积、和购买它时得到的打折优惠券数目,1 ≤ pi ≤ 10000, 1 ≤ ai ≤ 10000 , 0 ≤ ni < m。
l 随后是ni 对整数xi,j 和 yi,j 表示你得到打折的披萨序号xi,j (1 ≤ xi,j ≤ m, xi,j ≠ i) 以及买披萨xi,j得到的折扣yi,j,以百分比表示(1 ≤ yi,j≤ 50)。可以假设对于每个i ,xi,j 两两不同。
输出
对于每个测试用例:
打印一行,为通过一次购买任何一种披萨所能得到的单位面积最低价钱. 四舍五入到小数点后4位。
注意到你能组合任意数目的打折优惠券:对于价格为10的披萨和两个折扣为50和20的优惠券,你将只需支付10 * 0.8 * 0.5 = 4单位的钱。
样例输入
1
80 30 0
2
200 100 1 2 50
200 100 0
5
100 100 2 3 50 2 50
100 100 1 4 50
100 100 1 2 40
600 600 1 5 10
1000 10 1 1 50
0
样例输出
2.6667
1.5000
0.5333
题目来源
“IBM南邮杯”团队赛2009
题意不是很难,看到n=15我尝试了暴力、递归两种方法,T了之后乖乖地状压dp去了,用一个二进制数表示当前状态s,其中‘1’表示已经买过的pizza种类,然后依次去掉一个‘1’,得到s',由s'可以转移得到s,dp[te]=mini(dp[te],dp[o]+p[k]*zhe[o][k] ); 其中p[k]代表第k种pizza的价格,zhe[o][k]代表的是第k种物品在状态s'时所能获得的所有折扣,dp[te]表示状态s时最小的总价格。。。。然后,,,,就是一路优化常数的悲催经历了,,,感谢陈老师和蔡神,没有他们,,,我调一个星期都弄不出来,,,,
优化:
1.预处理了每个状态s时,第i种pizza所能得到的总折扣率。
2.去掉了位运算
3.预处理了每个状态中0的位置,0的总数
4.优化了dp顺序
5.还有各种稀奇古怪的,,,反正刚好不T了,我也就没管了,,,
附上题解:(来自陈老师)
Since each pizza can be bought at most once, we could solve the problem by enumerating all subsets of pizzas. However the order of buying the pizzas does matter, since coupons cannot be used retrospectively, and it is too slow to try all permutations of how to buy the pizzas.
The important observation is: given the subset of pizzas already bought, we do not care anymore about the order in which this subset of pizzas was bought. Therefore, we can use dynamic programming to solve this problem. We represent the subset of pizzas already bought by a bitmask with n bits, where bit i is set to 1 if and only if pizza i was already bought. The recurrence relation can be formulated as follows: minimum_price(mask) = minmask = submask + 2i minimum_price(submask) + price of pizza i where the price of pizza i is determined using discount coupons given for the pizzas bought before pizza i represented by the bitmask submask.
When calculating the minimum price for a subset of pizzas we only need to refer to bitmasks with a lower numeric value, therefore the recurrence relation can be calculated bottom up by enumerating all bitmasks with n bits from 0 to 2n-1. We need a table of size 2n, and each entry of the table can be calculated in O(n2) time, therefore the overall runtime is O(2n ⋅ n2).
Judges' test data consists of 37 test cases; most of them are random-generated.
ps:最后三个是我的代码,前面ac的都是蔡神的
| 5954 | njczy2010 | G | Accepted | 12656 KB | 921 ms | GCC | 2612 B | 2014-11-27 22:08:52 | 
| 5953 | njczy2010 | G | Accepted | 12656 KB | 890 ms | G++ | 2823 B | 2014-11-27 22:07:44 | 
| 5951 | njczy2010 | G | Accepted | 12656 KB | 921 ms | GCC | 2823 B | 2014-11-27 22:05:53 | 
| 5949 | njczy2010 | G | Time Limit Exceed at Test 1 | GCC | 3044 B | 2014-11-27 21:55:45 | ||
| 5948 | njczy2010 | G | Time Limit Exceed at Test 1 | GCC | 3146 B | 2014-11-27 21:53:51 | ||
| 5947 | njczy2010 | G | Time Limit Exceed at Test 1 | GCC | 3150 B | 2014-11-27 21:47:58 | ||
| 5946 | njczy2010 | G | Time Limit Exceed at Test 1 | GCC | 2751 B | 2014-11-27 21:13:15 | ||
| 5924 | njczy2010 | G | Accepted | 9312 KB | 750 ms | GCC | 2223 B | 2014-11-27 16:23:23 | 
| 5918 | njczy2010 | G | Time Limit Exceed at Test 1 | GCC | 3162 B | 2014-11-27 16:14:19 | ||
| 5917 | njczy2010 | G | Time Limit Exceed at Test 1 | GCC | 2967 B | 2014-11-27 16:08:09 | ||
| 5916 | njczy2010 | G | Compile Error | GCC | 2965 B | 2014-11-27 16:07:51 | ||
| 5915 | njczy2010 | G | Time Limit Exceed at Test 1 | GCC | 2851 B | 2014-11-27 16:05:25 | ||
| 5913 | njczy2010 | G | Time Limit Exceed at Test 1 | GCC | 2806 B | 2014-11-27 16:03:56 | ||
| 5911 | njczy2010 | G | Accepted | 9312 KB | 859 ms | G++ | 2215 B | 2014-11-27 15:53:04 | 
| 5909 | njczy2010 | G | Time Limit Exceed at Test 1 | G++ | 2996 B | 2014-11-27 15:52:13 | ||
| 5908 | njczy2010 | G | Time Limit Exceed at Test 1 | G++ | 2692 B | 2014-11-27 15:26:46 | ||
| 5864 | njczy2010 | G | Time Limit Exceed at Test 1 | G++ | 2482 B | 2014-11-27 11:57:07 | ||
| 5863 | njczy2010 | G | Time Limit Exceed at Test 1 | G++ | 2428 B | 2014-11-27 11:55:09 | ||
| 5862 | njczy2010 | G | Time Limit Exceed at Test 1 | G++ | 2416 B | 2014-11-27 11:50:31 | ||
| 5861 | njczy2010 | G | Time Limit Exceed at Test 1 | G++ | 2406 B | 2014-11-27 11:44:55 | 
这是水过去的代码:
#include<stdio.h> //#include<pair> #define N 20
#define M 1005
#define mod 1000000007
//#define p 10000007
#define mod2 1000000000
#define ll long long
#define LL long long
#define eps 1e-9
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) int m;
int x[N][N];
double p[N],a[N],n[N],y[N][N];
double zhe[ ][N];
double ans;
double dp[ ];
double tta[ ];
int tot;
double zhezhe[N][N];
int pos[ ];
int son[ ];
int pos2[ ][ ];
int cnt2[ ];
int son2[ ][ ];
int h[];
char ss[];
int bit[][]; void ini1()
{
int i,o,j,k,ch; h[]=;
for(i=;i<;i++)
h[i]=*h[i-];
for(o=;o<;o++){
k=o;
j=;
ch=;
while(k > ){
bit[i][j] = k%;
k = k/;
j++;
}
for(j=;j<;j++)
{
if(bit[i][j]==)
{
pos2[o][ cnt2[o] ]=j;
cnt2[o]++;
}
else
{
if(ch==)
{
ch=;
pos[o]=j;
son[o]= o - h[j];
}
}
}
}
} void ini()
{
ans=;
int i,j;
int o;
tot=h[m];
for(i=;i<=m;i++){
for(j=;j<=m;j++){
zhezhe[i][j]=1.0;
}
scanf("%lf%lf%lf",&p[i],&a[i],&n[i]);
for(j=;j<=n[i];j++){
scanf("%d%lf",&x[i][j],&y[i][j]);
y[i][j]=(100.0-y[i][j])/100.0;
zhezhe[i][ x[i][j] ]=y[i][j];
}
} for(o=;o<tot;o++){
dp[o]=; }
for(j=;j<=m;j++){
zhe[][j]=1.0;
}
tta[]=0.0;
} void solve()
{
int o,k,kk,j;
int te;
dp[]=;
for(o=;o<tot;o++){
if(o>=){
j=pos[o]+;
te=son[o];
tta[o]=tta[te]+a[j]; for(kk=;kk<cnt2[o];kk++){
k=pos2[o][kk]+;
zhe[o][k]=zhe[te][k]*zhezhe[j][k];
}
} for(kk=;kk<cnt2[o];kk++){
k=pos2[o][kk]+;
te=o+h[k-];
dp[te]=mini(dp[te],dp[o]+p[k]*zhe[o][k] );
}
}
} void out()
{
int o;
for(o=;o<tot;o++){
//printf(" o=%d dp=%.4f\n",o,dp[o]);
ans=mini(ans,dp[o]/tta[o]);
}
printf("%.4f\n",ans);
} int main()
{
ini1();
// freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
while(scanf("%d",&m)!=EOF)
{
if( m== ) break; ini();
solve();
out();
} return ;
}
| RunID | User | Problem | Result | Memory | Time | Language | Length | Submit Time | 
|---|---|---|---|---|---|---|---|---|
| 5860 | njczy2010 | G | Accepted | 9312 KB | 734 ms | GCC | 2215 B | 2014-11-27 11:44:14 | 
| 5859 | njczy2010 | G | Time Limit Exceed at Test 1 | GCC | 2215 B | 2014-11-27 11:43:42 | ||
| 5858 | njczy2010 | G | Time Limit Exceed at Test 1 | G++ | 2404 B | 2014-11-27 11:36:29 | ||
| 5857 | njczy2010 | G | Time Limit Exceed at Test 1 | G++ | 3036 B | 2014-11-27 11:24:57 | ||
| 5856 | njczy2010 | G | Time Limit Exceed at Test 1 | G++ | 3466 B | 2014-11-27 11:13:39 | ||
| 5854 | njczy2010 | G | Time Limit Exceed at Test 1 | G++ | 3413 B | 2014-11-27 11:07:27 | ||
| 5849 | njczy2010 | G | Time Limit Exceed at Test 1 | G++ | 3722 B | 2014-11-27 10:23:56 | ||
| 5848 | njczy2010 | G | Compile Error | G++ | 3716 B | 2014-11-27 10:23:19 | ||
| 5847 | njczy2010 | G | Time Limit Exceed at Test 1 | G++ | 3330 B | 2014-11-27 10:12:09 | ||
| 5846 | njczy2010 | G | Time Limit Exceed at Test 1 | G++ | 3322 B | 2014-11-27 10:09:51 | ||
| 5845 | njczy2010 | G | Time Limit Exceed at Test 1 | G++ | 3323 B | 2014-11-27 10:08:35 | ||
| 5844 | njczy2010 | G | Time Limit Exceed at Test 1 | G++ | 3156 B | 2014-11-27 10:05:04 | ||
| 5842 | njczy2010 | G | Time Limit Exceed at Test 1 | GCC | 2916 B | 2014-11-26 23:30:18 | ||
| 5841 | njczy2010 | G | Time Limit Exceed at Test 1 | G++ | 2986 B | 2014-11-26 23:23:18 | ||
| 5840 | njczy2010 | G | Wrong Answer at Test 1 | G++ | 2986 B | 2014-11-26 23:22:42 | ||
| 5839 | njczy2010 | G | Wrong Answer at Test 1 | G++ | 2984 B | 2014-11-26 23:19:33 | ||
| 5838 | njczy2010 | G | Wrong Answer at Test 1 | G++ | 2982 B | 2014-11-26 23:18:46 | ||
| 5837 | njczy2010 | G | Time Limit Exceed at Test 1 | G++ | 2981 B | 2014-11-26 23:07:33 | ||
| 5836 | njczy2010 | G | Time Limit Exceed at Test 1 | G++ | 2715 B | 2014-11-26 22:58:10 | ||
| 5830 | njczy2010 | G | Time Limit Exceed at Test 1 | G++ | 2167 B | 2014-11-26 22:28:24 | 
NOJ 1116 哈罗哈的大披萨 【淡蓝】 [状压dp+各种优化]的更多相关文章
- Codevs  2009 大dota英雄  2013年省队选拔赛辽宁(状压DP)
		
2009 大dota英雄 2013年省队选拔赛辽宁 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 话说退役后的生活好无聊啊,以 ...
 - 状压dp大总结1  [洛谷]
		
前言 状态压缩是一种\(dp\)里的暴力,但是非常优秀,状态的转移,方程的转移和定义都是状压\(dp\)的难点,本人在次总结状压dp的几个题型和例题,便于自己以后理解分析状态和定义方式 状态压缩动态规 ...
 - [胡泽聪 趣题选讲]大包子环绕宝藏-[状压dp]
		
Description 你有一个长方形的地图,每一个格子要么是一个障碍物,要么是一个有一定价值的宝藏,要么是一个炸弹,或者是一块空地.你的初始位置已经给出.你每次可以走到上.下.左.右这四个相邻的格子 ...
 - DevExpress ChartControl大数据加载时有哪些性能优化方法
		
DevExpress ChartControl加载大数据量数据时的性能优化方法有哪些? 关于图表优化,可从以下几个方面解决: 1.关闭不需要的可视化的元素(如LineMarkers, Labels等) ...
 - 大页内存(HugePages)在通用程序优化中的应用
		
今天给大家介绍一种比较新奇的程序性能优化方法-大页内存(HugePages),简单来说就是通过增大操作系统页的大小来减小页表,从而避免快表缺失.这方面的资料比较贫乏,而且网上绝大多数资料都是介绍它在O ...
 - 解决MOFH免费空间cpanel面板大文件无法解压的情况
		
解决办法: 解压大文件,重新压缩为tar格式的压缩格式,这样可以更小,而且也可以在cpanel面板解压,记得使用filezilla软件上传文件,在cpanel的网页界面不要刷新,一刷新就不可以解压大文 ...
 - [百家号]看完再也不会被坑!笔记本接口大揭秘:HDMI、DP、雷电
		
看完再也不会被坑!笔记本接口大揭秘:HDMI.DP.雷电 https://baijiahao.baidu.com/s?id=1577309281431438678&wfr=spider& ...
 - Atitit 提升开发进度大方法--高频功能与步骤的优化 类似性能优化
		
Atitit 提升开发进度大方法--高频功能与步骤的优化 类似性能优化 1. 通用功能又可以组合成crud模块1 1.1. 查询(包括步骤,发送查询dsl,通讯返回结果,绑定到表格控件)2 1.2. ...
 - MySQL大数据量分页查询方法及其优化
		
MySQL大数据量分页查询方法及其优化 ---方法1: 直接使用数据库提供的SQL语句---语句样式: MySQL中,可用如下方法: SELECT * FROM 表名称 LIMIT M,N---适 ...
 
随机推荐
- WPF中引入外部资源
			
有时候需要在WPF中引入外部资源,比如图片.音频.视频等,所以这个常见的技能还是需要GET到. 第一步:在VS中创建一个WPF窗口程序 第二步:从外部引入资源,这里以引入图片资源为例 1)新建Reso ...
 - 优化SQL语句的方法
			
首先,对于where语句的注意事项: 1.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如:select id from t where nu ...
 - Eclipse调试:项目在Debug模式下,无法启动的问题
			
问题:Eclipse中调试Java项目时,使用正常模式:Run 项目名,可以正常启动.当想打断点调试时,点击Debug按钮后,项目显示 Source not found,或者弹出窗口显示服务器在45秒 ...
 - shell基础笔记1
			
---恢复内容开始--- 1 test命令中不能使用浮点小数值,如: A=1.444444:[ $A -gt 1 ] 2 test命令中的>或<必须转义,否则shell会把它 ...
 - vue axios 请求本地接口端口不一致出现跨域设置代理
			
首先在config下面的index.js,设置跨域代理 在axios请求的时候 用'/api/' 替代baseURL 最重要的就是设置完必须重新 npm run dev 否则不生效
 - Zookeeper 集群 BindException: Cannot assign requested address 解决方案
			
前言 经历: 最近在搭建zookeeper集群,基础是3台机器(尝试过ubuntu 17 和 Centos 7). 一开始选择的是3台腾讯云服务器,每台机器在java环境配置正确的情况下,单机的情况都 ...
 - 暴力解说之首次部署NGINX
			
前言 本章基于Centos 7.x系统讲解 本章讲解下在项目上线部署的时候对NGINX的操作.有些童鞋在网上百度类似LNMP安装就跟着命令一条一条执行了,如果没报错还好,一旦报错就懵逼状态了.这是对自 ...
 - POJ:2753-Seek the Name, Seek the Fame
			
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Description The little cat is s ...
 - redis配置cluster分布式集群
			
#下载最新的redis5. wget http://download.redis.io/releases/redis-5.0.3.tar.gz .tar.gz cd redis- make make ...
 - f-Strings:一种改进Python格式字符串的新方法
			
好消息是,F字符串在这里可以节省很多的时间.他们确实使格式化更容易.他们自Python 3.6开始加入标准库.您可以在PEP 498中阅读所有内容. 也称为“格式化字符串文字”,F字符串是开头有一个f ...