Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2), problem: (D) Field expansion Solution In C/C++

#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
#define f(i,j,k) for(int i=j;i<k;i++)
const int maxn = 1e5 + 10;
int A[maxn], a,b,h,w,n,ans;

void dfs(ll aa, ll bb, int cnt) {
if (cnt==n+1) return;
if (aa>=a && bb>=b) ans=min(ans,cnt);
if (A[cnt]==2) {
while (aa<a) aa*=2, cnt++;
while (bb<b) bb*=2, cnt++;
ans=min(ans, cnt);
}
if (aa<a) dfs(aa*A[cnt],bb,cnt+1);
if (bb<b) dfs(aa, bb*A[cnt], cnt+1);
}

int main() {
scanf(“%d %d %d %d %d”, &a,&b,&h,&w,&n);
ans=n+1;
f(i,0,n) scanf(“%d”, A+i);
sort(A,A+n, greater<int>());
dfs(h,w,0); dfs(w,h,0);
printf(“%d\n”, (ans==n+1)?-1:ans);
return 0;
}

(Visited 23 times, 1 visits today)

About the Author:

Leave A Comment