Codeforces 799D Field expansion(随机算法)

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

转载于:https://www.cnblogs.com/s1124yy/p/6852837.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值