codeforces 799 D. Field expansion(dfs+思维剪枝)
题目链接: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+思维剪枝)的更多相关文章
- Codeforces 799 D. Field expansion
题目链接:http://codeforces.com/contest/799/problem/D 因为${a_i>=2}$那么一个数字至多操作${log_{2}^{max(a,b)/min(h, ...
- 【codeforces 799D】Field expansion
[题目链接]:http://codeforces.com/contest/799/problem/D [题意] 给你长方形的两条边h,w; 你每次可以从n个数字中选出一个数字x; 然后把h或w乘上x; ...
- Codeforces 931D Peculiar apple-tree(dfs+思维)
题目链接:http://codeforces.com/contest/931/problem/D 题目大意:给你一颗树,每个节点都会长苹果,然后每一秒钟,苹果往下滚一个.两个两个会抵消苹果.问最后在根 ...
- codeforces 799 C. Fountains(二分+思维)
题目链接:http://codeforces.com/contest/799/problem/C 题意:要求造2座fountains,可以用钻石,也可以用硬币来造,但是能用的钻石有限,硬币也有限,问能 ...
- 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 ...
- 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 ...
- codeforces 615 B. Longtail Hedgehog (DFS + 剪枝)
题目链接: codeforces 615 B. Longtail Hedgehog (DFS + 剪枝) 题目描述: 给定n个点m条无向边的图,设一条节点递增的链末尾节点为u,链上点的个数为P,则该链 ...
- Codeforces 799D Field expansion(随机算法)
Field expansion [题目链接]Field expansion [题目类型]随机化算法 &题解: 参考自:http://www.cnblogs.com/Dragon-Light/p ...
- hdu6035[dfs+思维] 2017多校1
/*hdu6035[dfs+思维] 2017多校1*/ //合并色块, 妙啊妙啊 #include<bits/stdc++.h> using namespace std; ; const ...
随机推荐
- 【Java例题】5.1 多项式计算
1. 计算下列多项式的值. pn=an*x^n+...+a1*x+a0其中,"^"表示乘方. x.n以及ai(i=0,1,...,n-1)由键盘输入. package chapte ...
- java并发编程(二)----创建并运行java线程
实现线程的两种方式 上一节我们了解了关于线程的一些基本知识,下面我们正式进入多线程的实现环节.实现线程常用的有两种方式,一种是继承Thread类,一种是实现Runnable接口.当然还有第三种方式,那 ...
- 手写Struts,带你深入源码中心解析
个人剖析,不喜勿喷 扫码关注公众号,不定期更新干活 在此申明本博文并非原创,原文:http://blog.csdn.net/lenotang/article/details/3336623,本文章是在 ...
- 使用PowerShell 测试DNS
运行环境:Windows Server 2012 R2 获取服务器DNS命令,下面的仅获取一个dns (nslookup sql.ciras.com)[1].split(':')[1].trim() ...
- 分布式存储——ceph 的 python 基础接口
python 使用 boto 库完成分布式存储读.写.判断接口 import boto import boto.s3.connection from boto.s3.key import Key im ...
- Ubuntu下安装php7.1的gd,mysql,pdo_mysql扩展库
执行以下命令 # apt-get install php7.1-gd # apt-get install php7.0-mysql 重新启动 php7.1-fpm(因为我是安装的 Nginx 和 ph ...
- UI 组件 | Button
最近在与其他自学 Cocos Creator 的小伙伴们交流过程中,发现许多小伙伴对基础组件的应用并不是特别了解,自己在编写游戏的过程中也经常对某个属性或者方法的用法所困扰,而网上也没有比较清晰的用法 ...
- netty源码解解析(4.0)-18 ChannelHandler: codec--编解码框架
编解码框架和一些常用的实现位于io.netty.handler.codec包中. 编解码框架包含两部分:Byte流和特定类型数据之间的编解码,也叫序列化和反序列化.不类型数据之间的转换. 下图是编解码 ...
- 深入理解Nginx及使用Nginx实现负载均衡
前言: 最近在部署项目时要求实现负载均衡,有趣的是发现网上一搜全部都是以下类似的配置文件 upstream localhost{ server 127.0.0.1:8080 weight=1; ser ...
- HTML基础介绍
前言: H5是目前行业里非常热门的一种语言,学习难度较低,可以快速入门.我个人也在学习H5,采用写博客的方式来梳理我学到的知识点,也分享给更多想学习的人一起来学习. Tip:个人学习过程中梳理的知识点 ...