码迷,mamicode.com
首页 > 其他好文 > 详细

(记忆化DFS)Codeforces Round #413 D-Field expansion

时间:2017-05-14 17:58:03      阅读:310      评论:0      收藏:0      [点我收藏+]

标签:记忆化dfs   oal   parallel   turn   mod   nts   min   提高   ==   

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.

Input

The first line contains five integers abhw 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.

Output

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.

Example

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 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.

#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <stack>
#define mp make_pair
typedef long long ll;
typedef unsigned long long ull;
const int MAX=2e5+5;
const int INF=1e9+5;
const double M=4e18;
using namespace std;
const int MOD=1e9+7;
typedef pair<int,int> pii;
const double eps=0.000000001;
ll n, a, b, h, w;
int len[1000005];
int dp[40][MAX];
bool cmp(int a,int b)
{
return a>b;
}
int solve(int i,ll h,ll w)
{
    if((h>=a&&w>=b)||(h>=b&&w>=a))
        return 0;
if(i==n+1)
return INF;
    if(h> 200000 )
        h= 200000 ;
    if(w> 200000 )
        w= 200000 ;
    int &re=dp[i][h];
    if(re+1)
        return re;
    int re1=INF,re2=INF;
    if(h<a&&len[i]>1)
        re1=1+solve(i+1,h*len[i],w);
    if(w<b&&len[i]>1)
        re2=1+solve(i+1,h,w*len[i]);
    return re=min(re1,re2);

}

int main()
{
    //scanf("%d%d%d%d%d",&a,&b,&h,&w,&n);
  cin>>a>>b>>h>>w>>n;
    memset(dp,-1,sizeof(dp));
    for(int i=1;i<=n;i++)
        scanf("%d",&len[i]);
    sort(len+1,len+1+n,cmp);
    int an=solve(1,h,w);
    if(an<=1e5)
        printf("%d\n",an);
    else
        printf("-1\n");
    return 0;
}

很显然,先用长度大的,依次搜索即可,为提高效率加上记忆化。

(记忆化DFS)Codeforces Round #413 D-Field expansion

标签:记忆化dfs   oal   parallel   turn   mod   nts   min   提高   ==   

原文地址:http://www.cnblogs.com/quintessence/p/6852972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!