zoukankan      html  css  js  c++  java
  • (记忆化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
    ",an);
        else
            printf("-1
    ");
        return 0;
    }

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

  • 相关阅读:
    autoreleasepool
    #ifndef/#define/#endif
    类工厂创建单例
    第一篇献给你:Block的回调
    博客纪念日
    [系列教程] Discuz模板的制作方法
    使用Discuz!后台备份和恢复Discuz!站点数据库的方法教程
    discuz x2.5 还原教程
    80后公务员辞职自述:7年收入没涨 能力是听话
    公务员队伍开始动荡了吗?
  • 原文地址:https://www.cnblogs.com/quintessence/p/6852972.html
Copyright © 2011-2022 走看看