1141E-Superhero Battle (Thinking + Mathematics)

https://codeforces.com/problemset/problem/1141/E


Idea: Snail Climbing Well

But the dichotomous boundary is not adjusted

Direct mathematics O1 easy to handle

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[maxn],pre[maxn];
/*bool check(LL x,LL H,LL n){

    if(H+x*pre[n]<=0) return false;
    H+=x*pre[n];
    for(LL i=1;i<=n;i++){
            H+=a[i];
            if(H<=0){
                return false;
            }
    }
    if(H>0) return true;
}*/
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL H,n;cin>>H>>n;
  for(LL i=1;i<=n;i++){
    cin>>a[i];
  }
  LL temp=H;
  for(LL i=1;i<=n;i++){
      temp+=a[i];
      if(temp<=0){
        cout<<i<<"\n";
        return 0;
      }
  }
  if(temp>=H){
    cout<<"-1"<<"\n";
    return 0;
  }
  for(LL i=1;i<=n;i++){
    pre[i]=pre[i-1]+a[i];
  }
  LL mx=1e18;
  for(LL i=1;i<=n;i++){
    if(mx>pre[i]){
        mx=pre[i];
    }
  }
  LL H2=H+mx;
  LL k=(H2+abs(pre[n])-1)/abs(pre[n]);
  H+=k*pre[n];
  LL ans=k*n;
  for(LL i=1;i<=n;i++){
     H+=a[i];
     ans++;
     if(H<=0){
        break;
     }
  }
  cout<<ans<<"\n";
  /*
  没调出来...
  LL H2=H+mx;
  debug(H2);
  LL l=1;LL r=1e15;
  while(l<r){///二分出最多的大循环圈数
    LL mid=(l+r)>>1;
    debug(mid);
    if(check(mid,H2,n)==true) l=mid+1;
    else r=mid-1;
  }
  debug(l);
  if(l*abs(pre[n])>=H) l--;
  LL ans=0;
  H+=l*pre[n];
  ans+=l*n;
  debug(H);
  for(LL i=1;i<=n;i++){
     H+=a[i];
     ans++;
     if(H<=0){
        break;
     }
  }
  cout<<ans<<"\n";*/
return 0;
}

 

Guess you like

Origin blog.csdn.net/zstuyyyyccccbbbb/article/details/114686181
Recommended