[GYM] Gym

来源:互联网 发布:snmpv3 trap java 编辑:程序博客网 时间:2024/05/16 12:53

Problem F. Folding

Input file:

folding.in

Output file:

folding.out

Time limit:

2 seconds

Memory limit:

256 megabytes

As you canremember, Alex is fond of origami. She switched from squares to rectangles, andrectangles are much more difficult to master. Her main interest is to determinewhat is the minimum possible number of folds required to transformW ×Hrectangle to w ×h one. The result of each fold shouldalso be rectangular, so it is only allowed to make folds that are parallel tothe sides of the rectangle.

Help Alex and write a program that determines the minimum requirednumber of folds. Input

Thefirst line of the input contains two integers W and H — the initialrectangle dimensions. The second line contains two more integersw and h — the target rectangle dimensions (1 ≤ W,H,w,h ≤ 109). Output

Output a single integer — the minimumrequired number of folds to transform the initial rectangle to the target one.

If the required transformation is notpossible, output −1.

Examples

folding.in

 

folding.out

2 7

2 2

2

 

10 6

4 8

2

 

5 5

1 6

-1

 

In the first example you should fold 2 × 7rectangle 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 be parallel to the rectangle sides).


给你两个长方形的长和宽,问你最少通过多少次折叠能让第一个长方形变成和第二个一毛一样。不能输出 -1。每次折叠只能平行于边长折叠且只能使边长为整数。



先考虑,加入我们把 L1 变成 L2 要怎么叠。  


我们每次最多可以把 L1 变成 (L1 + 1) / 2  ,即对折。


所以我们循环判断,当 L2 小于 (L1 + 1) / 2  的时候,我们把L1对折。

否则最多折一次就能变成 L2 了。



然后考虑做法。


设原长方形边长 x1, y1,目标长方形边长 x2, y2。


暴力枚举 x1 变成 x2, y1 变成 y2   以及  x1 变成 y2, y1 变成 x2  然后取一个最小就好了。。。


似乎也可以二分答案?


gym提交似乎要来一个读文件。。。


但是我没有

 


#include <bits/stdc++.h>using namespace std;int n, m, x, y;int get_ans(){    int a1 = n;    int b1 = m;    int c1 = x;    int d1 = y;    int a2 = n;    int b2 = m;    int c2 = x;    int d2 = y;    int cnt1, cnt2;    cnt1 = cnt2 = 0;    if(a1 < c1 || b1 < d1){        cnt1 = 1e9 + 7;    }    else{        while((a1 + 1) >> 1 > c1){            cnt1 ++;            a1 = ((a1 + 1) >> 1);        }        if(a1 != c1){            cnt1 ++;        }        while((b1 + 1) >> 1 > d1){            cnt1 ++;            b1 = ((b1 + 1) >> 1);        }        if(b1 != d1){            cnt1 ++;        }    }    if(a2 < d2 || b2 < c2){        cnt2 = 1e9 + 7;    }    else{        while((a2 + 1) >> 1 > d2){            cnt2 ++;            a2 = ((a2 + 1) >> 1);        }        if(a2 != d2){            cnt2 ++;        }        while((b2 + 1) >> 1 > c2){            cnt2 ++;            b2 = ((b2 + 1) >> 1);        }        if(b2 != c2){            cnt2 ++;        }    }    if(cnt1 == 1e9 + 7 && cnt2 == 1e9 + 7){        return -1;    }    else{        return min(cnt1, cnt2);    }}int main(){    int l, r;    while(scanf("%d%d%d%d", &n, &m, &x, &y) == 4){        printf("%d\n", get_ans());    }    return 0;}




原创粉丝点击