【题目链接】:http://codeforces.com/contest/799/problem/D

【题意】



给你长方形的两条边h,w;

你每次可以从n个数字中选出一个数字x;

然后把h或w乘上x;

直到能够把一个长为a宽为b的长方形装下为止;

问你最小的数字选择次数;

【题解】



把所给的n个数字从大到小排;

显然同样是选一个数字,选大的数字肯定比较优;

问题只是要让哪一条边乘上它;

这里可以知道

如果全都是2的话

最多需要34个数字;

因为log2(100000)≈17

然后两条边都最多需要17个数字乘;

所以是34个数字;

但要给34个数字配的话;

复杂度是2^34;这是不合适的;

但是注意到;

如果从某一位开始之后,都是2;

那么就不存在分配问题了;

即分配给谁都是一样的了,因为都是乘2了;

而之前都是大于2的;也就是至少为3;

而log3(100000)≈11

也就是说等于2的数字所花费的时间可以近似忽略掉;

直接while处理一下就好;

而大于2的;最多22个;

而2^22是可以接受的了,只有400W左右;

写个dfs,从某一位开始如果变成2,后面就不再继续dfs,直接贪心能分配就分配;

这里的dfs写成逆序的,即把乘的变成除的,这样写起来方便一点.



【Number Of WA】



2



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100; int a,b,h,w,n,c[N],ans; void dfs(int a,int b,int num)
{
if (!a && !b)
{
ans = min(ans,num);
return;
}
if (num>=n) return;
if (c[num+1]==2)
{
while (a) a/=2,num++;
while (b) b/=2,num++;
ans = min(ans,num);
return;
}
if (a) dfs(a/c[num+1],b,num+1);
if (b) dfs(a,b/c[num+1],num+1);
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
//init??????
cin >> a >> b >> h >> w >> n;
rep1(i,1,n)
cin >> c[i];
sort(c+1,c+1+n,[&](int a,int b){return a>b;});
ans = n + 1;
dfs((a-1)/h,(b-1)/w,0);
dfs((b-1)/h,(a-1)/w,0);
cout << (ans==n+1?-1:ans)<<endl;
return 0;
}

【codeforces 799D】Field expansion的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【25.64%】【codeforces 570E】Pig and Palindromes

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【codeforces 754B】 Ilya and tic-tac-toe game

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【33.33%】【codeforces 586D】Phillip and Trains

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  6. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  7. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  8. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  9. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

随机推荐

  1. Dubbo&Zookeeper运行原理

    Dubbo是一个分布式服务框架,Dubbo的架构如图所示: 节点角色说明: Provider: 暴露服务的服务提供方. Consumer: 调用远程服务的服务消费方. Registry: 服务注册与发 ...

  2. phpEXCEL如何设置单元格格式为百分比

    $objExcel->getActiveSheet()->getStyle('C9')->getNumberFormat()->setFormatCode(PHPExcel_S ...

  3. 洛谷 P3014 [USACO11FEB]牛线Cow Line

    P3014 [USACO11FEB]牛线Cow Line 题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 The N (1 <= N <= 20) ...

  4. PowerDesigner16 破解

    近期开发项目,涉及到实体设计这块的时候,用的是PowerDesigner16,使用是挺方便的,可是存在一个问题.那就是PowerDesigner16存在一个试用期的问题,过期就打不开了. 之前好多同学 ...

  5. Android之怎样给ListView加入过滤器

    给ListView加入文字过滤器: //this.getListView().setTextFilterEnabled(true);//可能报错用以下的 listView.setTextFilterE ...

  6. JSP页面标签

    1.EL表达式中empty的用法 EL表达式中empty的用法 <c:if test="${! empty key}">${key}</c:if> < ...

  7. codeforces 490 D Chocolate

    题意:给出a1*b1和a2*b2两块巧克力,每次可以将这四个数中的随意一个数乘以1/2或者2/3,前提是要可以被2或者3整除,要求最小的次数让a1*b1=a2*b2,并求出这四个数最后的大小. 做法: ...

  8. java连接mysql数据库增删改查操作记录

    1. 连接数据库.得到数据库连接变量 注意连接数据库的时候 (1)打开DB Browser 新建一个Database Driver,注意加入Driver JARs的时候加入的包,我的是mysql-co ...

  9. HDU 5672 String 尺取法追赶法

    String Problem Description There is a string S.S only contain lower case English character.(10≤lengt ...

  10. Linux Mint (应用软件— 虚拟机:Virtualbox续)

    我已经在当前的电脑中安装好了虚拟机.并且在虚拟机中安装了Ubuntu14.04LTS系统.接下来能够開始自己的折腾之旅了. 開始使用的时候总是感觉显示有问题,根据经验来看同,是系统分辨率设置不当引起的 ...