Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) D. Field expansion

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.

没时间看的D居然是道暴力可以过的题唉,想着后面很难就没做,确实CF这种两小时赛制的比较机灵,有人觉得A难读,分数不高就放弃A,后面的题倒是容易看出他让你做什么。这个题的意思是你有一块h*w的

地经过放大ai倍使其不小于a*b,这不是直接dfs都可以过么,有些人以为这个扩大是相加,可能是读错题啦,这种写法类似于记忆化搜索

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n;
int tar[N];
int ans = n + 1;
bool cmp(int a, int b)
{
    return a > b;
}
void dfs(int cur_a, int cur_b, int step)
{
    if (!cur_a && !cur_b)
    {
        ans = min(ans, step);
        return;
    }
    if (step == n)
        return;
    if (tar[step] == 2)
    {
        while (cur_a) cur_a /= 2, step++;
        while (cur_b) cur_b /= 2, step++;
        ans = min(ans, step);
        return;
    }
    if (cur_a) dfs(cur_a / tar[step], cur_b, step + 1);
    if (cur_b) dfs(cur_a, cur_b / tar[step], step + 1);
}
int main()
{
    int a, b, h, w;
    while (~scanf("%d%d%d%d%d", &a, &b, &h, &w, &n))
    {
        ans = n + 1;
        for (int i = 0; i < n; i++)
            scanf("%d", tar + i);
        sort(tar, tar + n, cmp);
        dfs((a - 1) / h, (b - 1) / w, 0);
        dfs((a - 1) / w, (b - 1) / h, 0);
        ans == n + 1 ? printf("-1\n") : printf("%d\n", ans);
    }
    return 0;
}

 

posted @ 2017-05-12 16:07  暴力都不会的蒟蒻  阅读(207)  评论(0编辑  收藏  举报