题目链接:
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?
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.
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.
6 3
4
8 5
3
22 4
6
题意:
把变成为x的等边三角形变成边长为y的三角形,其中过程要求三角形恒成立,最少需要操作多少次;
思路:
反过来贪心;
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #include <stack> #include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar()); for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar()); F && (num=-num); } int stk[70], tp; template<class T> inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + '0'); putchar(' '); } const int mod=1e9+7; const double PI=acos(-1.0); const LL inf=1e18; const int N=(1<<20)+10; const int maxn=1e5+110; const double eps=1e-12; int x,y; queue<int>qu; int main() { read(x);read(y); int ans=0; qu.push(y);qu.push(y);qu.push(y); while(1) { int f=qu.front();qu.pop(); int g=qu.front();qu.pop(); int u=qu.front();qu.pop(); if(f>=x&&g>=x&&u>=x)break; ans++; qu.push(g);qu.push(u);qu.push(g+u-1); } cout<<ans<<endl; return 0; }