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 ...
随机推荐
- powermockito单元测试之深入实践
概述 由于最近工作需要, 在项目中要做单元测试, 以达到指定的测试用例覆盖率指标.项目中我们引入的powermockito来编写测试用例, JaCoCo来监控单元测试覆盖率.关于框架的选择, 网上讨论 ...
- CentOS 7.3下使用YUM 安装MySQL5.6
1.检查Linux系统中是否已安装 MySQL rpm -qa | grep mysql 返回空值的话,就说明没有安装 MySQL 注意:在新版本的CentOS7中,默认的数据库已更新为了Mariad ...
- 无法正常卸载pr
控制面板找不到pr和ps,根本无法卸载,我试过官方工具没用,也试过ccleaner,也检测不到?
- 我的mybatis入门宝典
**********************************************************************************************一:myba ...
- 有关element 的一些问题(随时更新)
<el-input></el-input> input 组件中官方自带的change时间是监听失去焦点之后的value变化,要想一只监听value的值变化的话需要使用 @i ...
- 简单认识Nginx---负载均衡
中大型项目都会考虑到分布式,前面几篇文章着重介绍了数据处理的技术集群.今天来研究一下关于服务器的负载均衡–Nginx.他除了静态资源的处理外还有可以决定将请求置于那台服务上. Nginx的安装 点我下 ...
- 【KakaJSON手册】01_JSON转Model_01_基本用法
在iOS开发中,后台返回的数据大多是JSON格式,对应地会被网络框架层解析成Swift中的Dictionary.Array.由于数据类型的复杂.字段的繁多,直接使用Dictionary.Array会比 ...
- 【Aizu - 2249】Road Construction(最短路 Dijkstra算法)
Road Construction Descriptions Mercer国王是ACM王国的王者.他的王国里有一个首都和一些城市.令人惊讶的是,现在王国没有道路.最近,他计划在首都和城市之间修建道路, ...
- 5G标准公布,你很快又要换手机了
通常,在4G网络环境下,下载一部1G的电影只需要30秒时间,对于经历过2G和3G网络的我们来说已经非常快了. 但是听说,5G环境中下载一部同样的电影,根本不是用秒来计算的,甚至有外媒说,5G的速率会是 ...
- Dubbo(一):Dubbo运行原理
前言: 在开始入门Javaweb时,学的基本都是MVC开发模式,一个项目基本上就是model,view,controller三层.但是随着系统的服务逐渐加多,SOA模式更加适合目前项目开发.而SOA模 ...