Codeforces 799D Field expansion - 搜索 - 贪心
In one of the games Arkady is fond of the game process happens on a rectangular field. In the game process Arkady can buy extensions for his field, each extension enlarges one of the field sizes in a particular number of times. Formally, there are n extensions, the i-th of them multiplies the width or the length (by Arkady's choice) by ai. Each extension can't be used more than once, the extensions can be used in any order.
Now Arkady's field has size h × w. He wants to enlarge it so that it is possible to place a rectangle of size a × b on it (along the width or along the length, with sides parallel to the field sides). Find the minimum number of extensions needed to reach Arkady's goal.
The first line contains five integers a, b, h, w and n (1 ≤ a, b, h, w, n ≤ 100 000) — the sizes of the rectangle needed to be placed, the initial sizes of the field and the number of available extensions.
The second line contains n integers a1, a2, ..., an (2 ≤ ai ≤ 100 000), where ai equals the integer a side multiplies by when the i-th extension is applied.
Print the minimum number of extensions needed to reach Arkady's goal. If it is not possible to place the rectangle on the field with all extensions, print -1. If the rectangle can be placed on the initial field, print 0.
3 3 2 4 4
2 5 4 10
1
3 3 3 3 5
2 3 5 4 2
0
5 5 1 2 3
2 2 3
-1
3 4 1 1 3
2 3 2
3
In the first example it is enough to use any of the extensions available. For example, we can enlarge h in 5 times using the second extension. Then h becomes equal 10 and it is now possible to place the rectangle on the field.
题目大意 给定一个w × h的矩形,每次可以将w或h乘上ai(ai不能重复使用,并且与顺序无关)。问至少扩充多少次可以使得这个矩形中包含一个a × b的子矩形,如果无解输出-1。
因为 $w$ 和 $h$ 呈指数级增长,考虑爆搜。
可以知道题目要求最少的,所以先用大的ai更划算。因此可以先将ai排一次序。
既然是求最少,又确定是搜索,考虑 bfs。加上判重。好了这道题AC了。
不知道为啥原来写了一个假的复杂度证明还觉得它很对?
不难发现它涉及到的状态数不会超过下面的状态数乘 34。不过这也是一个非常松的界。
Code
/**
* Codeforces
* Problem#799D
* Accepted
* Time: 31ms
* Memory: 2500k
*/
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean; int a, b, c, d, n;
int* arr; typedef class Data {
public:
int nc;
int nd;
int step; Data():nc(), nd(), step() { }
Data(long long nc, long long nd, int step):step(step) {
this->nc = min(nc, (long long)a);
this->nd = min(nd, (long long)b);
} boolean isfin() {
return nc >= a && nd >= b;
} boolean operator < (Data b) const {
if(nc != b.nc) return nc < b.nc;
return nd < b.nd;
}
}Data; inline void init() {
scanf("%d%d%d%d%d", &a, &b, &c, &d, &n);
arr = new int[(n + )];
for(int i = ; i <= n; i++) {
scanf("%d", arr + i);
}
} set<Data> se;
queue<Data> que;
inline int bfs(int c, int d) {
Data s(c, d, );
if(s.isfin()) return ;
while(!que.empty()) que.pop();
que.push(s);
while(!que.empty()) {
Data e = que.front();
que.pop();
// printf("e:%d %d %d\n", e.nc, e.nd, e.step); if(e.nc < a) {
Data eu(e.nc * 1LL * arr[e.step + ], e.nd, e.step + );
// printf("%d %d %d\n", eu.nc, eu.nd, eu.step);
if(eu.isfin()) return eu.step;
if(eu.step < n && !se.count(eu))
que.push(eu), se.insert(eu);
} if(e.nd < b) {
Data eu(e.nc, e.nd * 1LL * arr[e.step + ], e.step + );
// printf("%d %d %d\n", eu.nc, eu.nd, eu.step);
if(eu.isfin()) return eu.step;
if(eu.step < n && !se.count(eu))
que.push(eu), se.insert(eu);
}
}
return -;
} inline void solve() {
sort(arr + , arr + n + , greater<int>());
while(n && arr[n] == ) n--;
int res1 = bfs(c, d), res2 = bfs(d, c);
if(res1 == - && res2 == -) puts("-1");
else if(res1 == - || res2 == -) printf("%d\n", max(res1, res2));
else printf("%d\n", min(res1, res2));
} int main() {
init();
solve();
return ;
}
Field expansion(bfs)
当 $a_i$ 大于2时,因为 $\log_3 10^5 \leqslant 11$,所以暴力枚举的次数不会超过 $2^{22}$,不过这是很松的界了,因为一边满了后不会再往这边放了。当 $a_i$ 等于2时,可以直接判。
Code
/**
* Codeforces
* Problem#799D
* Accepted
* Time: 15ms
* Memory: 2428k
*/
#include <bits/stdc++.h>
using namespace std;
#define smin(a, b) a = min(a, b)
typedef bool boolean;
const signed int inf = 1e9; int a, b, c, d, n;
int* arr; inline void init() {
scanf("%d%d%d%d%d", &a, &b, &c, &d, &n);
arr = new int[(n + )];
for(int i = ; i <= n; i++) {
scanf("%d", arr + i);
}
} int res = ;
int deplimit = ;
void dfs(int dep, int nc, int nd) {
// printf("%d %d %d\n", dep, nc, nd);
if(nc >= a && nd >= b) {
deplimit = dep, smin(res, dep);
// cout << dep << endl;
return;
}
if(dep == n) return;
if(arr[dep + ] == ) {
deplimit = dep;
while(nc < a && dep < n) nc <<= , dep++;
while(nd < b && dep < n) nd <<= , dep++;
if(nc < a || nd < b) return;
// cout << dep << endl;
smin(res, dep);
}
if(dep == deplimit) return;
long long nnc = nc * 1LL * arr[dep + ], nnd = nd * 1LL * arr[dep + ];
if(nc < a) dfs(dep + , smin(nnc, (long long)a), nd);
if(nd < b) dfs(dep + , nc, smin(nnd, (long long)b));
} inline void solve() {
sort(arr + , arr + n + , greater<int>());
while(n && arr[n] == ) n--;
dfs(, c, d), dfs(, d, c);
printf("%d\n", (res < ) ? (res) : (-));
} int main() {
init();
solve();
return ;
}
Codeforces 799D Field expansion - 搜索 - 贪心的更多相关文章
- Codeforces 799D Field expansion(随机算法)
Field expansion [题目链接]Field expansion [题目类型]随机化算法 &题解: 参考自:http://www.cnblogs.com/Dragon-Light/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 Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- [Codeforces 1214A]Optimal Currency Exchange(贪心)
[Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...
- [CQOI2012]模拟工厂 题解(搜索+贪心)
[CQOI2012]模拟工厂 题解(搜索+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1327574 链接题目地址:洛谷P3161 BZOJ P26 ...
- 【codeforces 799D】Field expansion
[题目链接]:http://codeforces.com/contest/799/problem/D [题意] 给你长方形的两条边h,w; 你每次可以从n个数字中选出一个数字x; 然后把h或w乘上x; ...
- 【动态规划】【滚动数组】【搜索】Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) D. Field expansion
显然将扩张按从大到小排序之后,只有不超过前34个有效. d[i][j]表示使用前i个扩张,当length为j时,所能得到的最大的width是多少. 然后用二重循环更新即可, d[i][j*A[i]]= ...
- Codeforces 799 D. Field expansion
题目链接:http://codeforces.com/contest/799/problem/D 因为${a_i>=2}$那么一个数字至多操作${log_{2}^{max(a,b)/min(h, ...
- 【贪心+DFS】D. Field expansion
http://codeforces.com/contest/799/problem/D [题意] 给定长方形的两条边h和w,你可以从给出的n个数字中随意选出一个x,把h或者w乘上x(每个x最多用一次) ...
随机推荐
- MyBatis基础入门《三》Select查询集合
MyBatis基础入门<三>Select查询集合 描述: 代码新增了一个MybatisUtil工具类,查询数据库返回集合的时候,接收数据的三种方式.由于代码会渐渐增多,未涉及改动过的文件不 ...
- node+react 打包成功,控制台报错
控制台报错: 'ReactCurrentOwner' of undefined 解决办法:RN版本的问题. As I mentioned, make sure you've installed the ...
- caffe-ssd运行create_data.sh的时候报错:SSD from caffe.proto import caffe_pb2 ImportError: No module named caffe.proto
在用voc2007和voc2012的数据训练基于caffe的SSD模型的时候,我们需要将图片数据转换成lmdb格式,运行脚本文件是SSD源码里面提供的create_data.sh(具体位置在$CAFF ...
- oauth2.0学习笔记(摘抄简化)
大量摘抄白话简明教程. 附:可以参考<RFC6749协议中文版及oauth2.0>文档 一.OAuth 白话简明教程 1.简述 http://www.cnblogs.com/Ceri/p/ ...
- D Tree Requests dfs+二分 D Pig and Palindromes -dp
D time limit per test 2 seconds memory limit per test 256 megabytes input standard input output stan ...
- Keep On Movin (贪心)
#include<bits/stdc++.h> using namespace std; int main(){ int T, n, a;scanf("%d",& ...
- Sqoop 学习之路
sqoop 基础知识和基本操作可以参考这篇博客:https://www.cnblogs.com/qingyunzong/p/8807252.html#_label3
- 以太坊智能合约开发,Web3.js API 中文文档 ethereum web3.js入门说明
以太坊智能合约开发,Web3.js API 中文文档 ethereum web3.js入门说明 为了让你的Ðapp运行上以太坊,一种选择是使用web3.js library提供的web3.对象.底层实 ...
- CAN学习网站
百度搜索:Controller Area Network http://www.esd-electronics-usa.com/Controller-Area-Network-CAN-Introduc ...
- java -cp & java jar的区别
java -cp java -cp 和 -classpath 一样,是指定类运行所依赖其他类的路径,通常是类库和jar包,需要全路径到jar包,多个jar包之间连接符:window上分号“;”.Lin ...