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最多用一次) ...
随机推荐
- c++基础:之泛型与标准模板库
- Hibernate框架的第四天
## Hibernate框架的第四天 ## ---------- **回顾:Hibernate框架的第三天** 1. 一对多关联关系映射 * JavaBean的编写 * 编写映射的配置文件 * 使用级 ...
- laravel学习历程
1.www目录下拉下来一个laravel框架 composer create-project laravel/laravel laravelapp --prefer-dist laravelapp 为 ...
- DX9 DirectX鼠标控制程序 代码
代码实例1: // @time: 2012.3.26 // @author: jadeshu // des: DirectX鼠标控制程序 #include <Windows.h> #inc ...
- Ubuntu10.04 python2.6下安装matplotlib环境
一.准备工作1.sudo apt-get install python-numpy2.sudo apt-get install python2.6-dev3.sudo apt-get install ...
- linux常用命令:mkdir 命令
linux mkdir 命令用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录. 1.命令格式: mkdir [选项] 目录... 2.命令 ...
- flask 使用Flask-SQLAlchemy管理数据库(连接数据库服务器、定义数据库模型、创建库和表)
使用Flask-SQLAlchemy管理数据库 扩展Flask-SQLAlchemy集成了SQLAlchemy,它简化了连接数据库服务器.管理数据库操作会话等各种工作,让Flask中的数据处理体验变得 ...
- flask设置cookie,设置session,模拟用户认证、模拟管理后台admin、模拟用户logout
设置cookie HTTP协议是无状态的,在一次请求响应结束后,服务器不会留下关于客户端状态的信息.但是对于某些web程序来说,客户端的信息有必要被记住,比如用户的登录状态,这样就可以根据用户的状态来 ...
- GUI界面修饰
function varargout = GUI20(varargin) % GUI20 MATLAB code for GUI20.fig % GUI20, by itself, creates a ...
- SQLServer将服务器A表写到服务器B表
不同服务器数据库之间的数据操作 --创建链接服务器 exec sp_addlinkedserver 'ITSV ', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 ' ...