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

Posted 惜取少年时

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(记忆化DFS)Codeforces Round #413 D-Field expansion相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 1363E - Tree Shuffling (思维/dfs/记忆化)

Codeforces 1363E - Tree Shuffling (思维/dfs/记忆化)

Codeforces 374 C. Travelling Salesman and Special Numbers (dfs记忆化搜索)

Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)

Codeforces Round #459 (Div. 2) C 思维,贪心 D 记忆化dp

Codeforces Round #536 E. Lunar New Year and Red Envelopes /// 贪心 记忆化搜索 multiset取最大项