C. Memory and De-Evolution 逆向思维

http://codeforces.com/contest/712/problem/C

要使得把三角形由边长n变成m,等价于由m变成n

如果是从n变成m,则很难判断每次判断变成多少。比如22的变成4,一开始那一条边变成什么都是可以得,都是满足三角形规律的。

但是如果你直接变成m,则后面的得不到最优。

变成22、22、4的话,后面的一路被限制。

那么如果你从4变到22,则每次选最大即可。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;


#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
int arr[33];
int n, m;
void work() {
    arr[1] = arr[2] = arr[3] = m;
    int ans = 0;
    while (true) {
        if (arr[1] == arr[2] && arr[2] == arr[3] && arr[3] == n) break;
        int mi = inf, id;
        for (int i = 1; i <= 3; ++i) {
            if (mi > arr[i]) {
                mi = arr[i];
                id = i;
            }
        }
        int haha1 = 0, haha2 = 0;
        for (int i = 1; i <= 3; ++i) {
            if (id == i) continue;
            if (!haha1) haha1 = arr[i];
            else haha2 = arr[i];
        }
        if (haha1 + haha2 - 1 >= n) {
            arr[id] = n;
        } else arr[id] = haha1 + haha2 - 1;
        ans++;
    }
    cout << ans << endl;
}

int main() {
#ifdef local
    freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
    IOS;
    while (cin >> n >> m) work();
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/liuweimingcprogram/p/6123989.html