CF799D:Field expansion(dp)

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

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


题意:给一个矩形a*b,和一个可扩展矩形h*w,从n个数里面任意分配若干个给h或w乘上,每个数只能使用一次,问最小使用多少个数这个矩形能装下a*b矩形。

思路:显然选择大的数有最优解,且最多需要34个数即可,因为2^17就超过题目范围的1e5了,那么对于数a[i],要么分配给h要么给w,用dp[i][j]表示处理前i个数h达到j长度时w的最大值。

//reference Dust_Heart
# include <bits/stdc++.h>
using namespace std;

typedef long long LL;
LL n, dp[38][100003], arr[100003];
int solve(LL a, LL b, LL h, LL w)
{
    int ans= -1;
    memset(dp, -1, sizeof(dp));
    dp[0][min(a,h)] = min(b, w);
    for(int i=1; i<=min(35,(int)n); ++i)
    {
        for(int j=1; j<=a; ++j)
        {
            if(dp[i-1][j]==-1) continue;
            LL nx = min(a, j*arr[i]);
            LL ny = min(b, dp[i-1][j]*arr[i]);
            dp[i][j] = max(dp[i][j], ny);
            dp[i][nx] = max(dp[i][nx], dp[i-1][j]);
        }
    }
    for(int i=1; i<36; ++i)
        if(dp[i][a]>=b)
        {
            ans = i;
            break;
        }
    return ans;
}
int main()
{
    LL a, b, h, w;
    while(~scanf("%I64d%I64d%I64d%I64d%I64d",&a,&b,&h,&w,&n))
    {
        for(int i=1; i<=n; ++i)
            scanf("%I64d",&arr[i]);
        if(h>=a&&w>=b || h>=b&&w>=a)
        {
            printf("0\n");
            continue;
        }
        sort(arr+1, arr+n+1,greater<LL>());
        int ans = min(solve(a, b, h, w), solve(a, b, w, h));
        if(ans == -1) puts("-1");
        else
            printf("%d\n",ans);
    }
    return 0;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: “note: in expansion of macro” 的意思是“注意:在宏展开中”。它通常出现在编程语言中,表示在宏定义中进行宏展开时需要注意的事项。在宏展开过程中,可能会出现一些意想不到的问题,需要仔细检查和调试。 ### 回答2: “Expansion of macro”(宏的展开)这个概念很常见于程序设计中,是指编写程序时使用的一种特殊的技术。在程序编写过程中,我们经常需要编写一些重复性的代码,这些代码里面有一些变量是可以重复利用的,为了解决这类问题,程序员通常会使用“宏”技术。 宏是一种可以自动替换一段代码的语句。在宏的使用过程中,程序员可以将一段代码封装起来,用宏的方式调用这段代码,使代码的可读性和可维护性都得到增强。 宏的展开是指,程序在运行时会将宏调用语句替换为对应的代码语句,这个过程就叫做宏的展开。展开过程一般是由编译器或解释器完成的,这个过程可以将程序编写时定义的宏自动转换成真正的代码片段。 因此,展开宏可以帮助程序员降低代码的复杂度,缩短代码的长度,最终提高程序的效率。同时,宏也是一种智能化的编程方式,可以在编写程序的过程中提高效率,减少出错的可能性和避免代码的重复性。 总之,“宏的展开”在程序设计中是一个非常重要的技术,它可以帮助程序员提高代码的可读性和可维护性,同时还能够帮助程序员有效地节约时间和精力,是程序开发中不可或缺的一部分。 ### 回答3: “在宏扩展中”意味着在计算机编程中使用了宏来扩展代码。宏是一种用于自动化代码编写的预处理器指令,它们允许程序员在编译代码之前对代码进行转换。使用宏可以简化代码编写,使代码更容易维护和修改。 在宏扩展的过程中,程序员可以使用预定义的宏或自己定义的宏。预定义的宏包括如下所示: 1. __FILE__:当前源文件的文件名。 2. __LINE__:当前源代码行号。 3. __DATE__:当前编译日期。 4. __TIME__:当前编译时间。 自定义宏通常使用#define指令定义。以下是一个简单的例子: #define MAX(x, y) ((x) > (y) ? (x) : (y)) 该宏定了一个比较大小的函数,可以比较两个数的大小并返回较大的值。 使用宏扩展时需要注意一些问题。例如,宏可以使代码更加难以阅读,因为它们可以使代码变得混乱且难以理解。此外,如果不小心定义了一个重复的宏,可能会导致代码出现错误,特别是在复杂的程序中。 总之,在宏扩展中,宏提供了一种有效的方法来自动化代码生成和转换。随着使用宏的经验增加,程序员可以更加熟练地使用宏来编写更高效、更易于维护的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值