HDU 4814 思维

HDU 4814
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4814
题意:
给一个黄金分割点的值,问怎么样把一个数(<=1e9)表示成以黄金分割点为进制的数。而且最后表示出来的数要进行标准化处理,即所有数字非0即1,且不能有两个1相邻。
思路:
复现的时候并没有细想。
两个性质f(n) = f(n - 1) + f(n - 2)-> 1 = f(-1) +f(-2),2*f(n) = f(n+1) + f(n - 2)(f代表黄金分割,里面参数代表幂次)
然后就可以把数字根据第一个性质划归到f(-1)和f(-2),根据两个性质来回折腾就可以。
自己写的时候开了个SPFA,妥妥TLE,因为无用状态太多。实际上考虑每次都会因为第二个性质最高位向前推进一位,而算出来最多有五十位,所以复杂度100*100。
源码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <string>
#include <queue>
using namespace std;
const int MAXN = 100 + 5;
int d[MAXN * 2];
struct D
{
    int u, v;
    bool operator < (const D& rbs)const{
        return v < rbs.v;
    }
};
priority_queue<D>que;
bool valid(int &l, int &r)
{
    int ok = 0;
    for(int i = l + 1 ; i <= r ; i++){
//        printf("i = %d\n", i);
        if(d[i] == 1 && d[i - 1] == 1){
            d[i] = 0, d[i - 1] = 0;
            d[i + 1]++;
            if(r < i + 1)   r = i + 1;
            if(i == l)  l = i + 1;
            ok = 1;
        }
        else if(d[i] > 1){
            que.push(D{i, d[i]});
            ok = 1;
        }
    }
    return ok;
}
void check(int l, int r)
{
    for(int i = l ; i <= r ; i++)   printf("%d", d[i]);
    printf("\n");
}
int main()
{
    int u;
    while(scanf("%d", &u) != EOF){
        if(u == 1){
            printf("1\n");
            continue;
        }
        memset(d, 0, sizeof(d));
        while(!que.empty()) que.pop();
        que.push(D{MAXN - 1, u});
        que.push(D{MAXN - 2, u});
        d[MAXN - 1] = d[MAXN - 2] = u;
        int head, rear;
        head = MAXN - 2, rear = MAXN - 1;
        int flag = 1;
        while(flag){
            flag = 0;
            for(int i = head ; i <= rear ; i++){
                if(d[i] > 1){
                    d[i + 1] += d[i] / 2;
                    d[i - 2] += d[i] / 2;
                    d[i] %= 2;
                    head = min(head, i - 2);
                    rear = max(rear, i + 1);
                    flag = 1;
                }
            }
            for(int i = head ; i <= rear ; i++){
                int temp = min(d[i], d[i - 1]);
                if(temp > 0){
                    d[i + 1] += temp;
                    d[i] -= temp;
                    d[i - 1] -= temp;
                    flag = 1;
                    rear = max(rear, i + 1);
                }
            }
            while(d[head] == 0) head++;
            while(d[rear] == 0) rear--;
        }
        while(d[head] == 0) head++;
        while(d[rear] == 0) rear--;
        for(int i = rear ; i >= head ; i--){
            printf("%d", d[i]);
            if(i == MAXN)   printf(".");
        }
        printf("\n");
//        printf("head = %d, rear = %d\n", head, rear);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值