【题目链接】: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. 【Codeforces Round #507 (Div. 2, based on Olympiad of Metropolises) A】Palindrome Dance

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] i从1..n/2循环一波. 保证a[i]和a[n-i+1]就好. 如果都是2的话填上min(a,b)*2就好 其他情况跟随非2的. ...

  2. CF894A QAQ

    CF894A QAQ 题意翻译 题目大意: 给定一个字符串,字符串长度<=100,现在要求出字符串中'QAQ'的个数,注意,'QAQ'可以不连续,只要有顺序就可以了,例如QABQ也有一个QAQ ...

  3. NEFU 119

    和上一题一样,注意除不尽为0 #include <iostream> #include <cstdio> #include <cstring> #include & ...

  4. POI对Excel单元格进行颜色设置

    POI对Excel单元格进行颜色设置 学习了:http://www.myexception.cn/program/1932587.html HSSFWorkbook workbook = new HS ...

  5. Android Drawable 那些不为人知的高效使用方法

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/43752383,本文出自:[张鸿洋的博客] 1.概述 Drawable在我们平时的 ...

  6. mysql改动用户password

    登录root用户 用root用户登录控制台. use mysql use mysql,mysql是mysql数据库自己主动创建的一个数据库. 改动user表的数据 update user set pa ...

  7. spark视频教程免费下载

    下载地址:点我下载   其它章节陆续上传中,Hadoop视频教程正在整理中,敬请关注.

  8. Google C++ style guide——格式

    1.行长度 每一行代码字符数不超过80. 例外: 1)假设一行凝视包括了超过80字符的命令或URL,出于复制粘贴的方便能够超过80字符: 2)包括长路径的能够超出80列,尽量避免: 3)头文件保护能够 ...

  9. 深入分析JavaWeb Item39 -- 监听器(Listener)学习进阶

    一.监听域对象中属性的变更的监听器 域对象中属性的变更的事件监听器就是用来监听 ServletContext, HttpSession, HttpServletRequest 这三个对象中的属性变更信 ...

  10. C# DispatcherTimer Start之后立即执行

    如果DispatherTimer 的Interval 不是在实例化时赋值,那么Start之后,Tick方法会立即执行一次. DispatcherTimer timer = new Dispatcher ...