【Codeforces 1141E】Superhero Battle

【链接】 我是链接,点我呀:)
【题意】

题意

【题解】

二分最后轮了几圈。 二分之后直接o(N)枚举具体要多少时间即可。 注意爆long long的情况。 可以用对数函数,算出来有多少个0 如果大于17直接缩小点就好。

【代码】

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 2e5;

ll H,a[N+10];
int n;

int main(){
    ios::sync_with_stdio(0),cin.tie(0);
    cin >> H >> n;
    for (int i = 1;i <= n;i++) cin >> a[i];
    for (int i = 2;i <= n;i++) a[i]+=a[i-1];
    for (int i = 1;i <= n;i++)
        if (a[i]<=-H){
            cout<<i<<endl;
            return 0;
        }
    if (a[n]>=0){
        cout<<-1<<endl;
        return 0;
    }
    ll ans = 1e18;
    ll l = 0,r = 1e12,temp=-1;
    while (l<=r){
        ll mid = (l+r)/2;
        ll T1 = log10(mid+1);
        ll T2 = log10(abs(a[n])+1);
        if (T1+T2>=17){
                r = mid-1;
                continue;
        }
        ll rest = H-mid*abs(a[n]),temp1 = -1;
        if (rest<=0)
            temp1 = mid*n;
        else
            for (int i = 1;i <= n;i++){
                if (a[i]+rest<=0){
                    temp1 = i + mid*n;
                    break;
                }
            }
        if (temp1==-1){
            l = mid + 1;
        }else{
            temp = temp1;
            r = mid - 1;
        }
    }
    cout<<temp<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/10692076.html