【题目链接】: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



【完整代码】

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define lson l,m,rt<<1
  4. #define rson m+1,r,rt<<1|1
  5. #define LL long long
  6. #define rep1(i,a,b) for (int i = a;i <= b;i++)
  7. #define rep2(i,a,b) for (int i = a;i >= b;i--)
  8. #define mp make_pair
  9. #define pb push_back
  10. #define fi first
  11. #define se second
  12. #define ms(x,y) memset(x,y,sizeof x)
  13. typedef pair<int,int> pii;
  14. typedef pair<LL,LL> pll;
  15. const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
  16. const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
  17. const double pi = acos(-1.0);
  18. const int N = 1e5+100;
  19. int a,b,h,w,n,c[N],ans;
  20. void dfs(int a,int b,int num)
  21. {
  22. if (!a && !b)
  23. {
  24. ans = min(ans,num);
  25. return;
  26. }
  27. if (num>=n) return;
  28. if (c[num+1]==2)
  29. {
  30. while (a) a/=2,num++;
  31. while (b) b/=2,num++;
  32. ans = min(ans,num);
  33. return;
  34. }
  35. if (a) dfs(a/c[num+1],b,num+1);
  36. if (b) dfs(a,b/c[num+1],num+1);
  37. }
  38. int main()
  39. {
  40. //freopen("F:\\rush.txt","r",stdin);
  41. ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
  42. //init??????
  43. cin >> a >> b >> h >> w >> n;
  44. rep1(i,1,n)
  45. cin >> c[i];
  46. sort(c+1,c+1+n,[&](int a,int b){return a>b;});
  47. ans = n + 1;
  48. dfs((a-1)/h,(b-1)/w,0);
  49. dfs((b-1)/h,(a-1)/w,0);
  50. cout << (ans==n+1?-1:ans)<<endl;
  51. return 0;
  52. }

【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. spring4和hibernate4.0.0的整合

    1.在myeclipse以下创建一个javaproject或者webproject,我创建的时webproject,用的myeclipse2013 2.导入spring的依赖包 3.导入hiberna ...

  2. NAT&amp;Port Forwarding&amp;Port Triggering

    NAT     Nat,网络地址转换协议.主要功能是实现局域网内的本地主机与外网通信.     在连接外网时,内部Ip地址须要转换为网关(一般为路由器Ip地址)(port号也须要对应的转换)     ...

  3. 编程求解,输入两个整数n和m,从数列1,2,3,……n中随意取几个数,使其和等于m。要求将所有的可能组合列出来

    import java.util.LinkedList; import java.util.Scanner; public class Main { private static LinkedList ...

  4. MySQL8.0修改临时密码

    解决MySQL8.0报错:Unknown system variable 'validate_password_policy' 一.问题描述 1.在安装MySQL8.0时,修改临时密码,因密码过于简单 ...

  5. 12.Matlab神经网络工具箱

    概述: 1 人工神经网络介绍 2 人工神经元 3 MATLAB神经网络工具箱 4 感知器神经网络 5 感知器神经网络 5.1 设计实例分析 clear all; close all; P=[ ; ]; ...

  6. (转载) listview实现微信朋友圈嵌套

    listview实现微信朋友圈嵌套 标签: androidlistview 2016-01-06 00:05 572人阅读 评论(0) 收藏 举报  分类: android(8)  版权声明:本文为博 ...

  7. Android Handler学习笔记

    已经习惯了挖坑不填,继续任性一下,周一到周五继续挖坑,每周六周日负责填坑. 1.从Android UI线程谈起 出于性能考虑,Android 中的UI操作并不是线程安全的,所以Android中规定只能 ...

  8. soapUI检查webServices接口的方法以及对自动触发线程的查询

    这几天需要熟悉接口传输过来的数据,因此会用到soapUI,但是没结果这个工具,然后百度了下,结合了下,下面是我对webservice在soapUI的展现: 1:其实说白了,就是我们不知道从接口里传输过 ...

  9. 面向对象和结构化程序设计的区别X

    面向对象和结构化程序设计的区别 结构化程序的概念首先是从以往编程过程中无限制地使用转移语句而提出的.转移语句可以使程序的控制流程强制性的转向程序的任一处,在传统流程图中,就是用上节我们提到的" ...

  10. dp入门(先摆在这里,之后细细读)

    网址转载链接:  http://bbs.chinaunix.net/thread-4094539-1-1.html 动态规划:从新手到专家 Hawstein翻译 前言 我们遇到的问题中,有很大一部分可 ...