Codeforces Round #621 (Div. 1 + Div. 2)B Cow and Friend

第一感觉最小步数只和移动距离的最大值有关

对于任一移动距离a,水平移动距离范围0~a   而跳两步水平移动距离范围0~2a 且两步对称 就能回到x轴  这样就和纵坐标无关了

贪心策略:水平跳动amax,直到剩下距离amax<res<2amax或为0,显然可以两步到达

证明:水平跳动显然amax最优,对于剩下的距离只用amax需要两步 其他移动距离或组合也至少需要两步 因为amax<res<2amax 而amax又是最大移动距离

此时答案$max(2,ceil(\frac{a}{b}))$ 当然还存在一些可行解对于前一部分采取的策略可能是水平跳动其他距离或组合 代入前面的式子 显然不会比amax更优

注意:需要特判一步就能到的

官方:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=105;
int a[N];
int main(){
    int T;
    ios::sync_with_stdio(false);cin.tie(0);
    cin>>T;
    while(T--){
        int n,d;
        cin>>n>>d;
        for(int i=1;i<=n;i++)cin>>a[i];
        int ans=a[1];
        for(int i=2;i<=n;i++)
         if((i-1)*a[i]<=d){
             d-=(i-1)*a[i];
             ans+=a[i];
         }
         else{
           ans+=d/(i-1);break;
             
         }
        cout<<ans<<endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/wyh447154317/p/12334120.html