Codeforces 799D Field expansion(随机算法)

时间:2022-08-25 16:26:52

Field expansion

【题目链接】Field expansion

【题目类型】随机化算法

&题解:

参考自:http://www.cnblogs.com/Dragon-Light/p/6843866.html

这种想法简直让我大开眼界啊, 原来这题还可以这么写!!

&代码:

#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <ctime>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
#define fo(i,a,b) for(int i=(a);i<=(b);i++)
#define fd(i,a,b) for(int i=(a);i>=(b);i--)
const int maxn = 1e5 + 7;
int h, w, c, d, n, a[maxn];
ll ans;
void sol(ll x, ll y) {
ll cnt = 0;
while(x < h || y < w) {
cnt++;
if(cnt > n) return ;
if(x >= h) {
y *= a[cnt];
}
else if(y >= w) {
x *= a[cnt];
}
else {
if(rand() % 2) {
x *= a[cnt];
}
else {
y *= a[cnt];
}
}
}
ans = min(ans, cnt);
}
bool cmp(int a, int b) { return a > b;}
int main() {
freopen("E:1.in", "r", stdin);
srand(time(NULL));
scanf("%d%d%d%d%d", &h, &w, &c, &d, &n);
fo(i, 1, n) scanf("%d", &a[i]);
ans = INF;
int zz = 0;
sort(a + 1, a + 1 + n, cmp);
while((double)clock() / CLOCKS_PER_SEC <= 0.93) {
sol(c, d); sol(d, c);
zz++;
}
if(ans == INF) ans = -1;
printf("%d\n", ans);
// cout << zz << endl;
return 0;
}