题目链接:http://codeforces.com/contest/799/problem/D

题意:给出h*w的矩阵,要求经过操作使得h*w的矩阵能够放下a*b的矩阵,操作为:将长或者宽*z[i]

有n个z[i]而且每个z[i]只能用一次。

题解:首先我们知道最少要扩大几倍,

x = a / h + (a % h ? 1 : 0);

y = b / w + (b % w ? 1 : 0);

当然要先排一下序从大到小,然后再是for一遍

pp *= z[i];

如果pp>=x*y就是可行。

然后就是dfs,这里dfs直接暴力解决就行但还是需要一点剪枝的,由于会出现重复的z,dfs会出现重复情况这里需要剪一下。

#include <iostream>
#include <cstring>
#include <algorithm>
#define inf 0X3f3f3f3f
using namespace std;
typedef long long ll;
const int M = 1e5 + 10;
ll z[M] , x , y;
bool cmp(int a , int b) {
return a > b;
}
//last表示上次去的z
bool solve(int pos , ll cx , ll cy , ll last , int type) {
if(pos == -1) {
if(cx >= x && cy >= y) return true;
return false;
}
if(type != 1 || z[pos] != last) {
if(solve(pos - 1 , cx * z[pos] , cy , z[pos] , 0)) return true;
}
return solve(pos - 1 , cx , cy * z[pos] , z[pos] , 1);
//使用last的原因就是由于,z会有重复出现导致dfs出现重复。
}
int main() {
ll a , b , h , w , n;
cin >> a >> b >> h >> w >> n;
for(int i = 0 ; i < n ; i++) {
cin >> z[i];
}
sort(z , z + n , cmp);
x = a / h + (a % h ? 1 : 0);
y = b / w + (b % w ? 1 : 0);
ll pp = 1 , ans = inf;
if(x == 1 && y == 1) {
ans = 0;
}
else {
for(int i = 0 ; i < n ; i++) {
pp *= z[i];
if(pp >= x * y) {
if(solve(i , (ll)1 , (ll)1 , (ll)-1 , -1)) {
ans = min(ans , (ll)(i + 1));
}
}
}
swap(h , w);
x = a / h + (a % h ? 1 : 0);
y = b / w + (b % w ? 1 : 0);
pp = 1;
if(x == 1 && y == 1) {
ans = 0;
}
for(int i = 0 ; i < n ; i++) {
pp *= z[i];
if(pp >= x * y) {
if(solve(i , (ll)1 , (ll)1 , (ll)-1 , -1)) {
ans = min(ans , (ll)(i + 1));
}
}
}
}
if(ans != inf) {
cout << ans << endl;
}
else {
cout << -1 << endl;
}
return 0;
}

codeforces 799 D. Field expansion(dfs+思维剪枝)的更多相关文章

  1. Codeforces 799 D. Field expansion

    题目链接:http://codeforces.com/contest/799/problem/D 因为${a_i>=2}$那么一个数字至多操作${log_{2}^{max(a,b)/min(h, ...

  2. 【codeforces 799D】Field expansion

    [题目链接]:http://codeforces.com/contest/799/problem/D [题意] 给你长方形的两条边h,w; 你每次可以从n个数字中选出一个数字x; 然后把h或w乘上x; ...

  3. Codeforces 931D Peculiar apple-tree(dfs+思维)

    题目链接:http://codeforces.com/contest/931/problem/D 题目大意:给你一颗树,每个节点都会长苹果,然后每一秒钟,苹果往下滚一个.两个两个会抵消苹果.问最后在根 ...

  4. codeforces 799 C. Fountains(二分+思维)

    题目链接:http://codeforces.com/contest/799/problem/C 题意:要求造2座fountains,可以用钻石,也可以用硬币来造,但是能用的钻石有限,硬币也有限,问能 ...

  5. Codeforces 799D Field expansion - 搜索 - 贪心

    In one of the games Arkady is fond of the game process happens on a rectangular field. In the game p ...

  6. 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 i ...

  7. codeforces 615 B. Longtail Hedgehog (DFS + 剪枝)

    题目链接: codeforces 615 B. Longtail Hedgehog (DFS + 剪枝) 题目描述: 给定n个点m条无向边的图,设一条节点递增的链末尾节点为u,链上点的个数为P,则该链 ...

  8. Codeforces 799D Field expansion(随机算法)

    Field expansion [题目链接]Field expansion [题目类型]随机化算法 &题解: 参考自:http://www.cnblogs.com/Dragon-Light/p ...

  9. hdu6035[dfs+思维] 2017多校1

    /*hdu6035[dfs+思维] 2017多校1*/ //合并色块, 妙啊妙啊 #include<bits/stdc++.h> using namespace std; ; const ...

随机推荐

  1. hdoj 1753 (Java)

    刚刚开始用Java,代码难免不够简洁. import java.math.BigDecimal; import java.util.Scanner; public class Main { publi ...

  2. Resource 使用详解

    极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...

  3. Keil5调试过程中遇到的一些警告和错误

    最近用keil5调试代码出了一些警告与错误,整理如下: 1.warning: #1295-D: Deprecated declaration run_c - give arg types void r ...

  4. js 双向绑定数据

    let aaa = []; let bbb = [1,2,3]; let ccc = [0,9,8]; aaa = bbb; //此时aaa与bbb被绑定(aaa指向bbb的指向) ,若使用push则 ...

  5. ext container的使用的场景

    container 是 panel 简化,他称之为容器,而panel则是面板. 如果不需要类似Ext.panel.Panel,Ext.window.Window和Ext.tab.Panel 等功能,则 ...

  6. Kalman Filter、Extended Kalman Filter以及Unscented Kalman Filter介绍

    模型定义 如上图所示,卡尔曼滤波(Kalman Filter)的基本模型和隐马尔可夫模型类似,不同的是隐马尔科夫模型考虑离散的状态空间,而卡尔曼滤波的状态空间以及观测空间都是连续的,并且都属于高斯分布 ...

  7. spring aop 解决模糊查询参数 % - /等特殊符号问题

    import com.hsq.common.utils.StringUtil;import org.aspectj.lang.ProceedingJoinPoint;import org.aspect ...

  8. 2.PHP利用PDO连接方式连接mysql数据库

    代码如下 <?php$serverName = "这里填IP地址";$dbName = "这里填数据库名";$userName = "这里填用户 ...

  9. HTML/CSS:导航栏水平和垂直

    1.垂直导航栏 导航栏 = 链接列表导航栏基本上是一个链接列表,因此使用 <ul> 和 <li> 元素是非常合适的.如需构建垂直导航栏,我们只需要定义 <a> 元素 ...

  10. python之闭包+装饰器

    闭包 内部函数对外部函数作用域变量的引用. 函数内的属性都是有生命周期的,都是在函数执行期间 闭包内的闭包函数私有化了变量,类似于面向对象 图片解析 示例一 https://www.bilibili. ...