【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 [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- 【AIM Tech Round 5 (rated, Div. 1 + Div. 2) B】Unnatural Conditions
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 让a+b的和为100000000...0这样的形式就好了 这样s(a+b)=1<=m就肯定成立了(m>=1) 然后至于s ...
- rabbitMQ学习笔记(三) 消息确认与公平调度消费者
从本节开始称Sender为生产者 , Recv为消费者 一.消息确认 为了确保消息一定被消费者处理,rabbitMQ提供了消息确认功能,就是在消费者处理完任务之后,就给服务器一个回馈,服务器就会将 ...
- wpf获取webbroswer的两个方法
//跳转前的地址 private void WebBrowser_BeforeNavigate2(object pDisp, ref object URL, ref object Flags, ref ...
- 2015.04.29,外语,读书笔记-《Word Power Made Easy》 14 “如何谈论日常现象” SESSION 39
HOW TO TALK ABOUT COMMON PHENOMENA AND OCCURRENCES TEASER PREVIEW dire(['daiә(r)] adj. 可怕的,悲惨的,灾难警告的 ...
- hdoj--1166--敌兵布阵(线段树)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 最大似然估计的缺陷 —— 方差和均值的 bias
0. 均匀分布期望的最大似然估计 首先我们来看,如何通过最大似然估计的形式估计均匀分布的期望.均匀分布的概率密度函数为:f(x|θ)=1θ,0≤x≤θ.不失一般性地,将 x1,x2,-,xn 排序为顺 ...
- Java-杂项:Java数组Array和集合List、Set、Map
ylbtech-Java-杂项:Java数组Array和集合List.Set.Map 1.返回顶部 1. 之前一直分不清楚java中的array,list.同时对set,map,list的用法彻底迷糊 ...
- org/eclipse/jetty/util/component/Container$Listener
转自:https://blog.csdn.net/husheng8891/article/details/10200965?locationNum=9&fps=1 项目Demo开发的时候,发布 ...
- 常见DDOS攻击原理和防御
常见的DDOS攻击主要分为流量型攻击和cc攻击: 流量型攻击主要通过发送大量的非法报文侵占正常业务带宽 包括SYN Flood/ SYN-ACK Flood /ACK Flood/ FIN/RST F ...
- 31.QT坐标系
dialog.h #ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QLabel> #include ...