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;
}

#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;
}

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

Famed mountaineer Nirmal Purja, 43, among 10 climbers killed after avalanche in Pakistan

Tragedy in the Karakoram: Nirmal Purja Among 10 Lost in Avalanche In a devastating turn of events for the global mountaineering community, renowned high-altitude climber...

How Under Desk Storage Is Quietly Changing the Game

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Has a woman ever mated with an ape?

Throughout history, urban legends, mythology, and speculative fiction have frequently explored the boundaries between humans and animals. One of the most persistent and sensationalized...

Want to stay up to date with the latest news?

We would love to hear from you! Please fill in your details and we will stay in touch. It's that simple!