【codeforces 799D】Field expansion
【题目链接】:http://codeforces.com/contest/799/problem/D
【题意】
给你长方形的两条边h,w;
你每次可以从n个数字中选出一个数字x;
然后把h或w乘上x;
直到能够把一个长为a宽为b的长方形装下为止;
问你最小的数字选择次数;
【题解】
把所给的n个数字从大到小排;
显然同样是选一个数字,选大的数字肯定比较优;
问题只是要让哪一条边乘上它;
这里可以知道
如果全都是2的话
最多需要34个数字;
因为log2(100000)≈17
然后两条边都最多需要17个数字乘;
所以是34个数字;
但要给34个数字配的话;
复杂度是2^34;这是不合适的;
但是注意到;
如果从某一位开始之后,都是2;
那么就不存在分配问题了;
即分配给谁都是一样的了,因为都是乘2了;
而之前都是大于2的;也就是至少为3;
而log3(100000)≈11
也就是说等于2的数字所花费的时间可以近似忽略掉;
直接while处理一下就好;
而大于2的;最多22个;
而2^22是可以接受的了,只有400W左右;
写个dfs,从某一位开始如果变成2,后面就不再继续dfs,直接贪心能分配就分配;
这里的dfs写成逆序的,即把乘的变成除的,这样写起来方便一点.
【Number Of WA】
2
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100;
int a,b,h,w,n,c[N],ans;
void dfs(int a,int b,int num)
{
if (!a && !b)
{
ans = min(ans,num);
return;
}
if (num>=n) return;
if (c[num+1]==2)
{
while (a) a/=2,num++;
while (b) b/=2,num++;
ans = min(ans,num);
return;
}
if (a) dfs(a/c[num+1],b,num+1);
if (b) dfs(a,b/c[num+1],num+1);
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
//init??????
cin >> a >> b >> h >> w >> n;
rep1(i,1,n)
cin >> c[i];
sort(c+1,c+1+n,[&](int a,int b){return a>b;});
ans = n + 1;
dfs((a-1)/h,(b-1)/w,0);
dfs((b-1)/h,(a-1)/w,0);
cout << (ans==n+1?-1:ans)<<endl;
return 0;
}
【codeforces 799D】Field expansion的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【25.64%】【codeforces 570E】Pig and Palindromes
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 754B】 Ilya and tic-tac-toe game
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【33.33%】【codeforces 586D】Phillip and Trains
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- 使用Spring Initializer快速创建Spring Boot项目
目录结构 IDE都支持使用Spring的项目创建向导快速创建一个Spring Boot项目:选择我们需要的模块:向导会联网创建Spring Boot项目:默认生成的Spring Boot项目: 主程序 ...
- Web安全扫描工具
使用 Ibm security appscan 进行WEB安全扫描. 1.SQL注入: 2.发现内部IP泄露模式: 3.已解密的登录请求: 4.HTML注释敏感信息泄露:
- T4系列文章之2:T4工具简介、调试以及T4运行原理
一.前言 经过第一篇,我想大家现在对T4有了基本的印象,应该对T4有了一个大致的了解吧.现在,我们接着来讲一下T4的工具,然后下一篇我就开始T4的用法了.各位客官,就等了. 二.工具介绍 2.1 上图 ...
- Linux下I/O复用 Select与Poll
Select #include <sys/time.h>#include <sys/types.h>#include <sys/unistd.h> int sele ...
- HDU 3709
真是跪了,一看范围就不会往枚举的方向想,没想到真用枚举加剪枝了...->——-> 解释一下代码中的上限: 例如4567,当枚举最高位时,很明显不能超过4,所以有上限,但当最高位为3以下时, ...
- Java进化? Kotlin初探与集成Android项目
欢迎Follow我的GitHub, 关注我的CSDN. Kotlin是基于JVM的编程语言, 由JetBrains公司开发, 眼下已经开源. IntelliJ IDEA, PyCharm, Andro ...
- esql开发总结
1 定义或者声明方法 int method(char *arg1,char* arg2...); 实现方法 int method(char *arg1,char* arg2...) EXE ...
- 2015.05.05,外语,读书笔记-《Word Power Made Easy》 15 “如何谈论事情进展” SESSION 42
HOW TO TALK ABOUT WHAT GOES ON TEASER PREVIEW 一些以-ate结束的动词,通常表示: to exhaust([ig'zɔ:st] n. 排气,排气装置 v. ...
- 0x16 Trie
这章刷的真带劲 嘿嘿 裸题 #include<cstdio> #include<iostream> #include<cstring> #include<cs ...
- 点击了一个link button,查看后台调用
使用F12进行监视 本身是一个linkbutton,可以看到绑定了一个JavaScript <a id="gvStaticConnection_ctl02_fresh" hr ...