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 ...
随机推荐
- 提交bug的标准及书写规范
Bug有效性 1.交付过程中测试者需按照专家设定好的模块,对Bug进行归类提交: 2.Bug的类型默认为UI问题.功能问题.崩溃问题,提交Bug时不能弄错: 3.需求是否明确.前提条件是否满足.输入数 ...
- 调测Onvif事件总结解决办法
主要在调测事件用例的过程中,发现了大量的信息,和未曾碰到的场景和非法错误等信息,先总结解决办法如下: (1)测试过程中发现以前的一个难题解决了,原先在生成soap空间命名的文件中有部分需要下载,离线生 ...
- Unity经典游戏编程之:球球大作战
版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...
- 解决跨域session 同步问题
跨域来源:(前端站点和后端API布署到不同的站点) 解决方案 一.服务端设置 1.配置允许跨域请求 public class BaseAction { /** * 支持跨域请求 * @author f ...
- asp.net core系列 69 Amazon S3 资源文件上传示例
一. 上传示例 Install-Package AWSSDK.S3 -Version 3.3.104.10 using Amazon; using Amazon.Runtime; using Ama ...
- Web开发中的相对路径和绝对路径
在学习HTML的时候一定会遇到引入文件和链接跳转页面,比如:JS文件.CSS文件.Image图片.我们就会考虑是相对路径和绝对路径的问题.下面PHP程序员雷雪松就详细讲解下Web开发中的相对路径和绝对 ...
- .netcore持续集成测试篇之测试方法改造
系列目录 通过前面两节讲解,我们的测试类中已经有两个测试方法了,总体上如下 public class mvc20 { private readonly HttpClient _client; publ ...
- Docker之- 使用Docker 镜像和仓库
目录 使用Docker 镜像和仓库 什么是 Docker 镜像 列出 Docker 镜像 tag 标签 Docker Hub 拉取镜像 查找镜像 构建镜像 创建Docker Hub 账号 使用 Doc ...
- 数据库炸了——是谁动了我的wait_timeout
1.起因 隐约听到坐在我对面的测试说测试环境的接口有问题 他们一番商讨后,朝我这边反馈说,现在测试环境的接口报504 我条件反射的回了句那是接口超时,再多试几次(测试环境的性能比较差,尤其是数据库,经 ...
- (转载)分享常用的GoLang包工具
分享常用的GoLang包工具 包名 链接地址 备注 Machinery异步队列 https://github.com/RichardKnop/machinery Mqtt通信 github.com/e ...