http://codeforces.com/contest/799/problem/D

【题意】

给定长方形的两条边h和w,你可以从给出的n个数字中随意选出一个x,把h或者w乘上x(每个x最多用一次),直到能够把一个长为a宽为b的长方形装下为止。问最小的x选择次数。

首先,同样选一个数字,数字大的肯定较优,因此先给x从大到小排序;

现在的问题是同一个x,要给h乘还是w乘。

首先,题目的数据范围是100 000,所以最多只需要34个x(log100 000=17),但是如果这样暴搜的话时间复杂度是2^34,会超时。

但是,我们注意到,从某一位开始,后面都是2的话,就不需要再搜索了,因为2分配给谁都一样,只需要有一个while循环跑一下就可以了,这个剪枝可以把2^34减到2^22(log3(100 000)=11),这样就完美解决了这道题。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm> using namespace std;
const int maxn=1e5+;
int a,b,h,w,n;
int c[maxn];
int flag;
int ans;
bool cmp(int a,int b)
{
return a>b;
}
void dfs(int aa,int bb,int num)
{
if(!aa&&!bb)
{
ans=min(ans,num);
return;
}
if(num>=n)
{
return;
}
if(c[num]!=)
{
if(aa) dfs(aa/c[num],bb,num+);
if(bb) dfs(aa,bb/c[num],num+);
}
else
{
while(aa)
{
num++;
aa/=;
}
while(bb)
{
num++;
bb/=;
}
ans=min(ans,num);
return;
}
}
int main()
{
while(~scanf("%d%d%d%d%d",&a,&b,&h,&w,&n))
{
for(int i=;i<n;i++)
{
scanf("%d",&c[i]);
}
sort(c,c+n,cmp);
if(h>=a&&w>=b||h>=b&&w>=a)
{
printf("0\n");
continue;
}
ans=n+;
dfs((a-)/h,(b-)/w,);
dfs((a-)/w,(b-)/h,);
if(ans<=n)
{
printf("%d\n",ans);
}
else
{
printf("-1\n");
}
}
return ;
}

注意:

1. dfs不能得到一个值就认为是最优解.......

2.

 if(c[num]!=)
{
if(aa) dfs(aa/c[num],bb,num+);
if(bb) dfs(aa,bb/c[num],num+);
}

没有if判断会超时

【贪心+DFS】D. Field expansion的更多相关文章

  1. Codeforces 799D Field expansion - 搜索 - 贪心

    In one of the games Arkady is fond of the game process happens on a rectangular field. In the game p ...

  2. Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) D. Field expansion

    D. Field expansion time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  3. 小黑的镇魂曲(HDU2155:贪心+dfs+奇葩解法)

    题目:点这里 题目的意思跟所谓的是英雄就下100层一个意思……在T秒内能够下到地面,就可以了(还有一个板与板之间不能超过H高). 接触这题目是在昨晚的训练赛,当时拍拍地打了个贪心+dfs,果断跟我想的 ...

  4. Codeforces 799D Field expansion(随机算法)

    Field expansion [题目链接]Field expansion [题目类型]随机化算法 &题解: 参考自:http://www.cnblogs.com/Dragon-Light/p ...

  5. 【bzoj3252】攻略 贪心+DFS序+线段树

    题目描述 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏. 今天他得到了一款新游戏<XX半岛>,这款游戏有n个场景(scene),某 ...

  6. hdu6060[贪心+dfs] 2017多校3

    /* hdu6060[贪心+dfs] 2017多校3*/ #include <bits/stdc++.h> using namespace std; typedef long long L ...

  7. codeforces 799 D. Field expansion(dfs+思维剪枝)

    题目链接:http://codeforces.com/contest/799/problem/D 题意:给出h*w的矩阵,要求经过操作使得h*w的矩阵能够放下a*b的矩阵,操作为:将长或者宽*z[i] ...

  8. UVALive3902 Network[贪心 DFS&&BFS]

    UVALive - 3902 Network Consider a tree network with n nodes where the internal nodes correspond to s ...

  9. 【NOIP2003】传染病控制(-贪心/dfs)

    我自己yy了个贪心算法,在某oj 0msAC~.然后去wikioi提交,呵呵,原来是之前oj的数据太弱给我水过了,我晕. 我之前的想法是在这棵树上维护sum,然后按时间来割边,每一时刻割已经感染的人所 ...

随机推荐

  1. mvc不登录的情况下无法跳转至其他页面--解决方法之一

    在每个控制器里,加以下方法 /// <summary> /// 在调用视图之前拦截非法用户 /// </summary> /// <param name="fi ...

  2. Cocos工作两周感受

    我是一个专注搞Unity开发的程序猿哈哈,但是最近的项目要采用Cocos引擎开发.在迷茫和学习成长中已经不知不觉过了两周.我就简单谈谈我这两周学习Cocos的一个感受. 具体说公司是采用js语言来开发 ...

  3. 4. iOS测试常用方法

    1.    [XCUIElement exists]方法只能确定这个View是否存在,即使不在当前屏幕上也返回True.如果要确定View是否在屏幕可见范围内,可以判断其Frame是否在Window的 ...

  4. 解决python pip安装提示"not a supported wheel on this platform"

    python下载.whl安装文件后使用pip安装有时候会提示报错"xxxxx not a supported wheel on this platform",应该是下载的库文件版本 ...

  5. javaee 第14周

    1.web server Web Server中文名称叫网页服务器或web服务器.WEB服务器也称为WWW(WORLD WIDE WEB)服务器,主要功能是提供网上信息浏览服务.Web服务器可以解析( ...

  6. 禁用DRM

    10G: alter system set "_gc_policy_time"=0 scope=spfile sid='*'; alter system set "_gc ...

  7. dumpkeys - 转储显示键盘翻译表

    总览 (SYNOPSIS) dumpkeys [ -hilfn1 -Sshape -ccharset --help --short-info --long-info --numeric --full- ...

  8. import downloadjs from 'downloadjs' 如果是自己写的函数 没用默认导出 记得加花括号 例如 import { download } from './data.js'

    import downloadjs from 'downloadjs' 如果是自己写的函数 没用默认导出 记得加花括号 例如 import { download } from './data.js'

  9. com组件简单应用

    1.打开VS2010,新建ATL COM 项目,步骤:“文件” -->“新建” -->“项目”,选择“Visual C++” -->“ATL 项目” ,填写“名称” FirstCOM ...

  10. 第一个WebDriver脚本

    1.cmd下安装selenium,输入pip install selenium 2.下载Firefox浏览器的驱动程序,https://github.com/mozilla/geckodriver/r ...