730. 机器人跳跃问题

二分

问题显然具有二段性。

每一步过程中(E=2*E-h[i])(n)的范围为(10^5),中间过程可能溢出。

观察到如果中间某步满足(E ge maxh),则之后过程(E)的值一定非负,可以抵达终点。

const int N=1e5+10;
int h[N],maxh;
int n;

bool check(int e)
{
    for(int i=1;i<=n;i++)
    {
        e+=e-h[i];
        if(e < 0) return false;
        if(e >= maxh) return true;
    }
    return true;
}

int main()
{
    cin>>n;

    for(int i=1;i<=n;i++)
        cin>>h[i],maxh=max(maxh,h[i]);

    int l=0,r=maxh;
    while(l<r)
    {
        int mid=l+r>>1;
        if(check(mid)) r=mid;
        else l=mid+1;
    }

    cout<<l<<endl;
    //system("pause");
    return 0;
}

递推

(E_k=2E_{k-1}-H_k)(E_{k-1}=(E_k+H_k)/2),要求初始能量最小,且中间过程(E)非负,显然(E_n=0),从(E_n)开始逆推即可。

const int N=1e5+10;
int h[N];
int n;

int main()
{
    cin>>n;

    for(int i=1;i<=n;i++)
        cin>>h[i];
        
    double e=0;
    for(int i=n;i>=1;i--)
       e=(e+h[i])/2;
    cout<<ceil(e)<<endl;
    //system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/fxh0707/p/14551021.html