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 ...
随机推荐
- DesignPattern系列__03依赖倒置原则
依赖倒置原则(Dependence Inversion Priiciple,DIP) 介绍 High level modules should not depend upon low level mo ...
- Adapter适配器模式--图解设计模式
第二章: Adapter 模式 Adapter模式分为两种: 1.类适配器模式 2.委托适配器 我看的是<图解设计模式>这本书,这小鬼子说的话真难懂,只能好好看代码理解. 先说适配器模式要 ...
- CSS等分布局方法
原文链接:http://caibaojian.com/css-equal-layout.html CSS等比例划分,在CSS布局中是比较重要的,下面分享几种常用方法和探讨一下兼容性. 一:浮动布局+百 ...
- 初识JavaScript和面向对象
1.javascript基本数据类型: number: 数值类型 string: 字符串类型 boolean: 布尔类型 null: 空类型 undefault:未定义类型 object: 基本数据类 ...
- linux装OpenOffice后传---中文乱码的解决
上一篇的博客已经详细的介绍了linux系统上如何安装OpenOffice,安装之后使用发现转换的pdf出现中文乱码.后来发现是linux上没有中文对应的那个字体. 字体准备 在windows上的位置 ...
- 云上RDS架构
概述 越来越多的企业选择上云,最基础的云服务就是IaaS(Infrastructure as a Service)服务,直观理解就是虚拟主机,用户不用再自建机房,自己购买服务器,而是直接向云厂商购买虚 ...
- 编译Assimp傻瓜教程
assimp的编译过程和搭建OpenGL环境时glfw的编译基本相同,建议先阅读环境搭建 下载源码 这里使用的是3.3.1版本,Github下载assimp源码 解压完你会得到 接下来我们要编译这些源 ...
- Re-Architecting the Video Gatekeeper(二)
原文: https://medium.com/netflix-techblog/re-architecting-the-video-gatekeeper-f7b0ac2f6b00 想法 我们决定部署一 ...
- 谨慎 mongodb 关于数字操作可能导致类型及精度变化
1.问题描述 最近有一个需求,更新Mongo数据库中 原料 集合的某字段价格,更新后,程序报错了,说长度过长了,需要Truncation. 主要错误信息如下: FormatException: An ...
- 初学html总结
2019-08-17 17:58:49 html:超文本标记语言,用于网页结构的搭建 html语言构成:由标签.属性.属性值构成 标签:" < "后面第一个单词 属性:标签后 ...