zoukankan      html  css  js  c++  java
  • 【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;
    }
    
  • 相关阅读:
    Cocos2d-x 内存管理
    Cocos2d-x 解惑
    前端最实用、全面的工具类方法
    Java web 项目获取时间的方式列举
    Win系统常用指令
    Js三级下拉列表联动
    Js数组操作
    常用网站推荐
    最常用的正则表达式
    Oracle数据库学习
  • 原文地址:https://www.cnblogs.com/AWCXV/p/10692076.html
Copyright © 2011-2022 走看看