(记忆化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 42 5 4 10

Output

1

Input

3 3 3 3 52 3 5 4 2

Output

0

Input

5 5 1 2 32 2 3

Output

-1

Input

3 4 1 1 32 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;
}

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

时间: 2024-08-03 08:31:04

(记忆化DFS)Codeforces Round #413 D-Field expansion的相关文章

hdu1078 记忆化dfs

FatMouse and Cheese Problem Description FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) where 0 <= p < n and 0 <= q < n. At each grid location Fatmouse

HDU 1142 A Walk Through the Forest(dijkstra+记忆化DFS)

题意: 给你一个图,找最短路.但是有个非一般的的条件:如果a,b之间有路,且你选择要走这条路,那么必须保证a到终点的所有路都小于b到终点的一条路.问满足这样的路径条数 有多少,噶呜~~题意是搜了解题报告才明白的Orz....英语渣~ 思路: 1.1为起点,2为终点,因为要走ab路时,必须保证那个条件,所以从终点开始使用单源最短路Dijkstra算法,得到每个点到终点的最短路,保存在dis[]数组中. 2.然后从起点开始深搜每条路,看看满足题意的路径有多少条. 3.这样搜索之后,dp[1]就是从起

从DFS到记忆化DFS

从DFS到记忆化DFS到动态规划 什么是动态规划? 动态规划(Dynamic Programming)是通过组合子问题的解来解决问题的.动态规划是用于求解包含重叠子问题的最优化问题的方法.其基本思想是,将原问题分解为相似的子问题.在求解的过程中通过子问题的解求出原问题的解. 动态规划的分类: 1.       线性规划:拦截导弹,合唱队形,挖地雷等. 2.       区域规划:石子合并,加分二叉树,统计单词个数等. 3.       树形动规:贪吃的九头龙,二分查找树,聚会的欢乐等. 4.  

[HDU 1078]FatMouse and Cheese(记忆化DFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 题目大意:一个胖老鼠要在一个n*n大小的棋盘里吃奶酪,这个老鼠每一步最多能走k单位远,而且每走一步,必须走到比当前点奶酪数多的点那去.告诉你这个棋盘里每个点上的奶酪个数,求这个老鼠最多能吃多少奶酪. 思路:类似于棋盘DP的记忆化DFS,直接搜加记忆答案就可以了. #include <iostream> #include <stdio.h> #include <string.

*HDU1142 最短路+记忆化dfs

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7995    Accepted Submission(s): 2943 Problem Description Jimmy experiences a lot of stress at work these days, especiall

DFS Codeforces Round #306 (Div. 2) B. Preparing Olympiad

题目传送门 1 /* 2 DFS: 排序后一个一个出发往后找,找到>r为止,比赛写了return : 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <cstring> 7 #include <cmath> 8 #include <algorithm> 9 #include <vector> 10 #include <map> 11 #include

DFS Codeforces Round #299 (Div. 2) C. Tavas and Karafs

题目传送门 1 /* 2 DFS:按照长度来DFS,最后排序 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <iostream> 8 #include <cmath> 9 #include <vector> 10 using namespace std; 11 12 const int MAXN = 1e3 + 10; 1

HDU ACM 1078 FatMouse and Cheese 记忆化+DFS

题意:FatMouse在一个N*N方格上找吃的,每个点(x,y)有一些吃的,FatMouse从(0,0)的出发去找吃的,每次最多走k步,他走过的位置可以吃掉吃的,保证吃的数量在0-100,规定他只能水平或者垂直走,每走一步,下一步吃的数量需要大于此刻所在位置,问FatMouse最多可以吃多少东西. 需要对步数进行扩展. #include<iostream> using namespace std; #define N 101 #define max(a,b) ((a)>(b)?(a):(

Codeforces Round#413 Problem A - C

[写在前面感(乱)叹(七)人(八)生(糟)的话] 本想借此机会一口气玩到蓝名,结果,A题写炸(少判了一种情况),C题写炸(辜负了我5分钟狂敲出来的线段树),结果又掉Rating...内心好绝望... Problem#A Carrot Cakes vjudge链接[here] (偷个懒,cf链接就不给了) 题目大意是说,烤面包,给出一段时间内可以考的面包数,建第二个炉子的时间,需要达到的面包数,问建炉子是否合理. 玄学 & 智商题,可能是因为我智商不够,所以在我决定休息的时候被hank掉了...