CF Round #413( Div.1/2) Field expansion
来源:互联网 发布:加工中心斜面怎么编程 编辑:程序博客网 时间:2024/04/28 12:54
D. Field expansion
time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output
In one of the games Arkady is fond ofthe game process happens on a rectangular field. In the game process Arkady canbuy extensions for his field, each extension enlarges one of the field sizes ina particular number of times. Formally, there aren extensions, thei-th of them multiplies the width or the length (byArkady's choice) by ai. Each extensioncan't be used more than once, the extensions can be used in any order.
Now Arkady's field has sizeh × w. He wants toenlarge it so that it is possible to place a rectangle of size a × b on it (alongthe width or along the length, with sides parallel to the field sides). Findthe minimum number of extensions needed to reach Arkady's goal.
Input
The first line contains five integersa,b,h,w and n (1 ≤ a, b, h, w, n ≤ 100 000) — thesizes of the rectangle needed to be placed, the initial sizes of the field andthe number of available extensions.
The second line containsn integers a1, a2, ..., an (2 ≤ ai ≤ 100 000), where ai equals theinteger a side multiplies by when the i-th extension isapplied.
Output
Print the minimum number of extensions needed to reachArkady's goal. If it is not possible to place the rectangle on the field withall extensions, print-1. If the rectangle can be placed on theinitial field, print 0.
Examples
Input
3 3 2 4 4
2 5 4 10
Output
1
Input
3 3 3 3 5
2 3 5 4 2
Output
0
Input
5 5 1 2 3
2 2 3
Output
-1
Input
3 4 1 1 3
2 3 2
Output
3
Note
In the first example it is enough to use any of theextensions available. For example, we can enlarge h in 5 times using thesecond extension. Then h becomes equal 10 and it is nowpossible to place the rectangle on the field.
题意:有一个a*b大小的矩形A,还有个h*w大小的矩形B,给出一系列扩大倍数,你可以任意挑选,使矩形B的h或w扩大相应倍数(注意是或),求最小的扩大倍数个数,使得矩形B能够完全放在矩形A中。
思路:首先根据贪心的策略,我们肯定先选能够扩倍数大的那个数,故先进行排序,进一步观察数据,发现a和b都<=1e5,可以发现就算ai都是最小的2,h,w都是最小的1,那么也只要大约选择34个2就一定能满足条件,所以我们只要考虑前几个大的数即可。
(两种方法中,都是h,w交换位置求最优解,原因是有可能是,a,h匹配、b,w匹配或a,w匹配、b,h匹配)
方法一:动态规划 (dp[i][j]表示考虑到第i个数时,其中一条边的乘积为j时,另一条边乘积的最大值)
#include <bits/stdc++.h>using namespace std;#define mst(a,b) memset((a),(b),sizeof(a))#define f(i,a,b) for(int i=(a);i<(b);++i)#define ll long longconst int maxn = 100005;const int mod = 475;const ll INF = 0x3f3f3f3f;const double eps = 1e-6;#define rush() int T;scanf("%d",&T);while(T--)ll v[maxn];int n;ll dp[36][maxn];bool cmp(ll a,ll b){ return a>b;}int solve(ll a,ll b,ll w,ll h){ mst(dp,-1); dp[0][min(a,w)]=h; for(int i=0;i<min(35,n);i++) for(int j=0;j<=a;j++) { if(dp[i][j]!=-1) { ll nh=min(j*v[i],a); ll nw=min(dp[i][j]*v[i],b); dp[i+1][nh]=max(dp[i+1][nh],dp[i][j]); dp[i+1][j]=max(dp[i+1][j],nw); } } for(int i=0;i<=min(35,n);i++) { if(dp[i][a]>=b) return i; } return INF;}int main(){ ll a,b,h,w; while(cin>>a>>b>>h>>w>>n) { for(int i=0;i<n;i++) { cin>>v[i]; } sort(v,v+n,cmp); int ans=min(solve(a,b,h,w),solve(a,b,w,h)); if(ans==INF) printf("-1\n"); else printf("%d\n",ans); } return 0;}
方法二:DFS(dalao想出来的做法,因为用到的ai个数较少,直接进行爆搜)
#include <bits/stdc++.h>using namespace std;#define mst(a,b) memset((a),(b),sizeof(a))#define f(i,a,b) for(int i=(a);i<(b);++i)#define ll long longconst int maxn = 100005;const int mod = 475;const ll INF = 0x3f3f3f3f;const double eps = 1e-6;#define rush() int T;scanf("%d",&T);while(T--)ll v[maxn];int n,ans;ll a,b,h,w;bool cmp(ll a,ll b){ return a>b;}void dfs(int count,ll nw,ll nh,int pre){ if(count>=ans) return; if(nw>=a&&nh>=b) { ans=min(ans,count); return; } if(count>=min(35,n)) return; if(nw<a&&pre!=v[count]) dfs(count+1,nw*v[count],nh,pre); if(nh<b) dfs(count+1,nw,nh*v[count],v[count]);}int main(){ while(cin>>a>>b>>h>>w>>n) { for(int i=0;i<n;i++) { cin>>v[i]; } sort(v,v+n,cmp); ans=INF; dfs(0,h,w,0); dfs(0,w,h,0); if(ans==INF) printf("-1\n"); else printf("%d\n",ans); } return 0;}
- CF Round #413( Div.1/2) Field expansion
- Codeforces Round #413 Div. 2 D. Field expansion
- Codeforces Round #413 D. Field expansion (搜索)
- Codeforces Round #413 D. Field expansion (搜索)
- cf Round #280 (Div. 2) E. Vanya and Field 数学
- (Codeforces Round #413) Field expansion (搜索+思维优化)
- CF Round #428( Div.2)BCDE
- CF Round #413( Div.1/2) Fountains(线状数组查找)
- CF Round #256 (Div. 2)
- CF Round #260 (Div 2)
- CF Round #439 (Div 2)
- YMS Round #1 Div. 2 C Mowing the Field
- CF799D:Field expansion(dp)
- C. Developing Skills (CF Round #322 (Div.2) 贪心)
- CF Round #412( Div.2) Success Rate
- CF Round #424( Div.2) D. Office Keys
- CF Round #350 (Div. 2)D1. Magic Powder - 1(未完成)
- CF Round #422( Div.1/2) D. My pretty girl Noora
- mvn不是内部或外部命令,也不是可运行的程序或批处理文件
- 工具打包
- android手机返回桌面后,点击app图标,app重启
- mycat ER关系 取模分片试验
- ios
- CF Round #413( Div.1/2) Field expansion
- Reflect中MethodInfo使用方法
- 中共中央组织部关于干部双重管理工作若干问题的通知-组通字(1991)35号
- shell 别名和快捷键
- HTTP会话原理解释与应用
- Code Review 代码审查 不完全整理
- hadoop命令OutOfMemoryError GC
- 如何正确再次封装ajax,封装回调函数
- 华为OJ训练之0035-170112-字符串加解密