(Codeforces Round #413) Field expansion (搜索+思维优化)
来源:互联网 发布:开淘宝店怎么样 编辑:程序博客网 时间:2024/05/11 18:57
In one of the games Arkady is fond of the game process happens on a rectangular field. In the game process Arkady can buy extensions for his field, each extension enlarges one of the field sizes in a particular number of times. Formally, there are n extensions, the i-th of them multiplies the width or the length (by Arkady's choice) by ai. Each extension can't be used more than once, the extensions can be used in any order.
Now Arkady's field has size h × w. He wants to enlarge it so that it is possible to place a rectangle of size a × b on it (along the width or along the length, with sides parallel to the field sides). Find the minimum number of extensions needed to reach Arkady's goal.
The first line contains five integers a, b, h, w and n (1 ≤ a, b, h, w, n ≤ 100 000) — the sizes of the rectangle needed to be placed, the initial sizes of the field and the number of available extensions.
The second line contains n integers a1, a2, ..., an (2 ≤ ai ≤ 100 000), where ai equals the integer a side multiplies by when the i-th extension is applied.
Print the minimum number of extensions needed to reach Arkady's goal. If it is not possible to place the rectangle on the field with all extensions, print -1. If the rectangle can be placed on the initial field, print 0.
3 3 2 4 42 5 4 10
1
3 3 3 3 52 3 5 4 2
0
5 5 1 2 32 2 3
-1
3 4 1 1 32 3 2
3
In the first example it is enough to use any of the extensions available. For example, we can enlarge h in 5 times using the second extension. Then h becomes equal 10 and it is now possible to place the rectangle on the field.
只要h或者w大于a或者b即可,组合方式有两种,h> a&&w> b或者h> b&&w> a,搜索的时候每个值乘给谁又有两种情况,故普通搜索最终复杂度为
#include<cstdio>#include<iostream>#include<algorithm>#include<queue>#include<stack>#include<cstring>#include<string>#include<vector>#include<cmath>#include<map>using namespace std;typedef long long ll;#define mem(a,b) memset(a,b,sizeof(a))const int maxn = 2e6+5;const int ff = 0x3f3f3f3f;int a,b,h,w,n;ll c[maxn];bool cmp(int x,int y){return x> y;}int dfs(ll p,ll q,int x){if(p>= a&&q>= b)return x-1;if(x == n+1)return -1;if(c[x] == 2)//特判{while(x<= n&&p< a)p*= 2,x++;while(x<= n&&q< b)q*= 2,x++;if(p< a||q< b)return -1;return x-1;}int s1 = -1,s2 = -1;if(p< a)//只有p比a小的时候才对p乘继续搜s1 = dfs(p*c[x],q,x+1);if(q< b)s2 = dfs(p,q*c[x],x+1);if(s1 == -1||s2 == -1)return max(s1,s2);elsereturn min(s1,s2);}int main(){cin>>a>>b>>h>>w>>n;for(int i = 1;i<= n;i++)scanf("%I64d",&c[i]);sort(c+1,c+n+1,cmp);int ans1 = dfs(h,w,1);int ans2 = dfs(w,h,1);if(ans1 == -1||ans2 == -1)cout<<max(ans1,ans2)<<endl;elsecout<<min(ans1,ans2)<<endl;return 0;}
- (Codeforces Round #413) Field expansion (搜索+思维优化)
- Codeforces Round #413 D. Field expansion (搜索)
- Codeforces Round #413 D. Field expansion (搜索)
- Codeforces Round #413 Div. 2 D. Field expansion
- Codeforces D 题 Field expansion
- CF Round #413( Div.1/2) Field expansion
- Codeforces 799D Field expansion 暴力
- Codeforces 799D Field expansion【贪心+暴搜】
- Codeforces 799D. Field expansion 【DP】
- Codeforces Round #403 E. Underground Lab(思维+搜索)
- Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field(思维)
- Educational Codeforces Round 1 (D) 搜索(优化剪枝)
- Educational Codeforces Round 22 C. The Tag Game(思维 搜索)
- Codeforces Round #158 (Div. 2)D. Black and White Tree(搜索 + 思维)
- CF799D:Field expansion(dp)
- Codeforces Round #256 (Div. 2)思维训练
- Codeforces Round #262 (Div. 2)思维训练
- Codeforces Round #305 B题 思维+贪心
- 安装swoole
- [LeetCode]102. Binary Tree Level Order Traversal
- Hibernate关系映射多对多
- matlab 巴特沃斯滤波
- [java web基础]实现自动登录功能demo
- (Codeforces Round #413) Field expansion (搜索+思维优化)
- 【必看】Vim Tutor
- AsycnTack与URLConnection的结合
- 第四章
- 将json数据添加到数据库
- 【DSP】CCS 5.5的安装教程
- 矩阵快速幂(new 模板
- 用json方法使宽高同时变.html
- vue.js 部署中碰到的图片路径问题