Codeforces 712C Memory and De-Evolution

参考自:https://www.cnblogs.com/ECJTUACM-873284962/p/6379014.html

【题目链接】

C. Memory and De-Evolution

time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input

output:standard output

Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.

In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.

What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?

  • Input

The first and only line contains two integers x and y (3 ≤ y < x ≤ 100 000) — the starting and ending equilateral triangle side lengths respectively.

  • Output

Print a single integer — the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.

  • Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
  • Note

In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides ab, and c as (a, b, c). Then, Memory can do .

In the second sample test, Memory can do .

In the third sample test, Memory can do: 

.

【题意】

现有边长为x的等边三角形,Memory想要将其变成边长为y的等边三角形。

现规定Memory每秒能够改变一条边的大小,但要保证改变后的三条边仍能构成一个三角形。

问最少需要多少时间才能变为边长为y的等边三角形?

【分析】

从边长为6的等边三角形变为边长为3的等边三角形。先将一边变为3,则(6,6,3),如果再将一边变成3,则(6,3,3)并不能组成三角形,所以只能先变为(6,4,3)然后变为(3,4,3),最后变为(3,3,3),一共4步,所以输出4,此题很明显是贪心,直接贪就是了,用一个数记录需要的时间即可!此题需要注意的是最好选择逆推法,由y->x,这样就能保证合法且最快了!

【时间复杂度】O(n)

借鉴于:HERE


【代码实现】

#include<stdio.h>
int main(){
    int s[3];
    int x,y,i;
    while(1){
        scanf("%d%d",&x,&y);
        s[0]=s[1]=s[2]=y;
        for(i=0; s[0]<x||s[1]<x||s[2]<x; i++)
            s[i%3]=s[(i+1)%3]+s[(i+2)%3]-1;
        printf("%d
",i);
    }
    return 0;
} 
原文地址:https://www.cnblogs.com/cruelty_angel/p/10200942.html