Gym - 101142F Folding(折半)

来源:互联网 发布:jdk 7u80 windows x32 编辑:程序博客网 时间:2024/05/21 06:49

传送门

As you can remember, Alex is fond of origami. She switched from squares to rectangles, and rectanglesare much more difficult to master. Her main interest is to determine what is the minimum possiblenumber of folds required to transform W × H rectangle to w × h one. The result of each fold should alsobe rectangular, so it is only allowed to make folds that are parallel to the sides of the rectangle.Help Alex and write a program that determines the minimum required number of folds.InputThe first line of the input contains two integers W and H — the initial rectangle dimensions. The secondline contains two more integers w and h — the target rectangle dimensions (1 ≤ W, H, w, h ≤ 10^9).OutputOutput a single integerthe minimum required number of folds to transform the initial rectangle tothe target one.If the required transformation is not possible, output −1.Examplesfolding.in 2 72 210 64 85 51 6folding.out22-1In the first example you should fold 2 × 7 rectangle to 2 × 4, and then to 2 × 2.In the second example you should fold 10 × 6 rectangle to 10 × 4, then to 8 × 4, and rotate it to 4 × 8.In the third example it is impossible to fold 5 × 5 rectangle to 1 × 6 one (remember that folds must beparallel to the rectangle sides).

题目大意:

给了一个 WH 的大矩形和一个 wh 的小矩形,现在让你将这个大矩形进行折叠使其变成这个小矩形,求需要的最小步数(要求是这个小矩形不能在进行拆分了),如果不能完成转化输出 -1.

样例解释:
Input:

2 7
2 2

Output:
2

首先将 27 的矩形折叠成 24的矩形,然后在将 24 的矩形折叠成 22 的,不能上来就将 27 的矩形折叠成 22的因为还有 25的这是不允许的。

解题思路:

首先我们很容易想到是 1 的情况:就是当 max(w,h)>max(W,H) 的时候就输出 1;然后在来考虑能够折叠的情况,其实还是很容易想到的是折半来分,这样是最小的步数,然后需要满足的条件就是不能有比这个大的,那么如果当前边是奇数的话就 +1 然后除以 2,否则直接除以 2,然后我们就将(W,w)(H,h)都分别算一遍,然后在将 (H,w)(W,h)算一遍取最小值就 OK了。
My Code

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;typedef long long LL;const LL INF = 1e18+5;LL Solve(LL mx, LL mn){    if(mn > mx)        return INF;    LL cnt = 0;    while(mx > mn){        mx = (mx+1)>>1LL;        cnt++;    }    return cnt;}int main(){    ///freopen("folding.in","r",stdin);    ///freopen("folding.out","w",stdout);    LL w, h ,ww, hh;    while(cin>>w>>h>>ww>>hh){        LL TW = max(w,h);        LL TH = min(w,h);        LL tw = max(ww,hh);        LL th = min(ww,hh);        if(tw > TW){            puts("-1");            continue;        }        if(th > TH){            puts("-1");            continue;        }        ///cout<<max(Solve(TW, tw)+Solve(TH,th),Solve(TW,th)+Solve(TH,tw))<<endl;        cout<<min(Solve(TW, tw)+Solve(TH,th),Solve(TW,th)+Solve(TH,tw))<<endl;    }    return 0;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 一岁宝宝太好动怎么办 3岁宝宝太好动怎么办 孩子学习注意力不集中怎么办 1岁宝宝皮肤黄怎么办 三岁宝宝太皮怎么办 一岁宝宝太皮了怎么办 5岁宝宝学习太皮怎么办 2岁宝宝太皮了怎么办 身上的皮肤很干怎么办 小孩子挑食厌食不吃饭怎么办 1岁宝宝特别淘气怎么办 4岁宝宝有多动症怎么办 3岁宝宝有多动症怎么办 6个月婴儿睡眠少怎么办 7个月婴儿睡眠少怎么办 孩子好动注意力不集中怎么办 学生在课堂上爱讲话怎么办? 幼儿园老师圢小孩脸怎么办 宝宝上幼儿园坐不住怎么办 宝宝在幼儿园总是坐不住怎么办 早教课上宝宝坐不住怎么办? 孩子在幼儿园上课坐不住怎么办 小孩不会写拼音a怎么办 小朋友上课注意力不集中怎么办 一年级孩子上课爱说话怎么办 一年级小孩不听老师话怎么办 大班幼儿规则意识差怎么办 幼儿大班《打雷了怎么办》的教案 小孩上课不听讲到处乱跑怎么办 小孩子经常咬人好动怎么办 6个月宝宝好动怎么办 新生调皮被幼儿园退学怎么办 孩子在幼儿园太调皮怎么办 幼儿上课注意力不集中怎么办 幼儿上课一半要离开怎么办 八个月宝宝消化不良拉肚子怎么办 8个月的宝宝拉肚子怎么办 孕8个月拉稀怎么办 孩子调皮好动爱说话怎么办 宝宝8个月不会爬怎么办 怀孕8个月不想要了怎么办