Codeforces1141E(E题)Superhero Battle
Each round has the same scenario. It is described by a sequence of nn numbers: d1,d2,…,dnd1,d2,…,dn (−106≤di≤106−106≤di≤106). The ii-th element means that monster's hp (hit points) changes by the value didi during the ii-th minute of each round. Formally, if before the ii-th minute of a round the monster's hp is hh, then after the ii-th minute it changes to h:=h+dih:=h+di.
The monster's initial hp is HH. It means that before the battle the monster has HH hit points. Print the first minute after which the monster dies. The monster dies if its hp is less than or equal to 00. Print -1 if the battle continues infinitely.
The first line contains two integers HH and nn (1≤H≤10121≤H≤1012, 1≤n≤2⋅1051≤n≤2⋅105). The second line contains the sequence of integers d1,d2,…,dnd1,d2,…,dn (−106≤di≤106−106≤di≤106), where didi is the value to change monster's hp in the ii-th minute of a round.
Print -1 if the superhero can't kill the monster and the battle will last infinitely. Otherwise, print the positive integer kk such that kk is the first minute after which the monster is dead.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=+;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int t,n,m,k,q;
unsigned long long ans;
ll cnt,flag,temp,sum;
ll h;
int a[N];
int main()
{
cin>>h>>n;
ll H=h;
ll minl=INF;
for(int i=;i<=n;i++){
cin>>a[i];
sum+=a[i];
minl=min(minl,sum);
if(h+sum<=){//如果h加上第一轮的sum小于0,就输出sum值所在下标
cout<<i<<endl;
return ;
}
}
if(minl>=||sum>=){// 和最小序列为正数或一轮所有值的和为正数,输出-1,代表不能实现将hp扣为0及一下
cout<<-<<endl;
return ;
}
cnt=(H+minl)/-sum;//hp加上和最小序列后除-sum得整数轮数,为n-2 或n-1 ,取决于数是到 和最小序列最后一个,还是之前就使hp<=0
if((H+minl)%-sum)//正好最大负值+整数个sum使hp=0
cnt++; //为n-1 或n
ans=(unsigned long long)cnt*n;//轮数乘以每轮个数
h=H+sum*cnt;
if(h<=){ //和最小序列和sum相等,即正好整数个sum使h<=0
cout<<ans<<endl;
return ;
}
for(int i=;i<=n;i++){//累加最后一轮
h+=a[i];
if(h<=){
cout<<ans+i<<endl;
return ;
}
}
}
题意分析:输入一个整数H,代表血量,每一轮的攻击消耗血量或增加血量,求消耗完时是第多少次攻击。
Codeforces1141E(E题)Superhero Battle的更多相关文章
- E. Superhero Battle Codeforces Round #547 (Div. 3) 思维题
E. Superhero Battle time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #547 (Div. 3) E. Superhero Battle
E. Superhero Battle A superhero fights with a monster. The battle consists of rounds, each of which ...
- CF1141E Superhero Battle
A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly n minu ...
- E. Superhero Battle
链接 [https://codeforces.com/contest/1141/problem/E] 题意 怪物开始的生命值,然后第i分钟生命值的变化 问什么时候怪物生命值为非正 分析 有一个巨大的坑 ...
- The 2015 China Collegiate Programming Contest -ccpc-c题-The Battle of Chibi(hdu5542)(树状数组,离散化)
当时比赛时超时了,那时没学过树状数组,也不知道啥叫离散化(貌似好像现在也不懂).百度百科--离散化,把无限空间中无限的个体映射到有限的空间中去,以此提高算法的时空效率. 这道题是dp题,离散化和树状数 ...
- 【CF1141E】Superhero Battle
\[x*p\ge y\rightarrow x=\lfloor{{y-1}\over p}\rfloor+1\]
- 【Codeforces 1141E】Superhero Battle
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 二分最后轮了几圈. 二分之后直接o(N)枚举具体要多少时间即可. 注意爆long long的情况. 可以用对数函数,算出来有多少个0 如果大于 ...
- 南阳ccpc C题 The Battle of Chibi && hdu5542 The Battle of Chibi (树状数组优化+dp)
题意: 给你一个长度为n的数组,你需要从中找一个长度为m的严格上升子序列 问你最多能找到多少个 题解: 我们先对原序列从小到大排序,排序之后的序列就是一个上升序列 这里如果两个数相等的话,那么因为题目 ...
- 南阳ccpc C题 The Battle of Chibi 树状数组+dp
题目: Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried ab ...
随机推荐
- TIME_WAIT的危害与避免
time-wait的产生: 在TCP连接中四次挥手关闭连接时,主动关闭连接的一方(上图中时Client)会在发送最后一条ACK报文后维持一段时长2MSL(MSL指的是数据包在网络中的最大生存时间)的等 ...
- Handler 机制(一)—— Handler的实现流程
由于Android采用的是单线程模式,开发者无法在子线程中更新 UI,所以系统给我提供了 Handler 这个类来实现 UI 更新问题.本贴主要说明 Handler 的工作流程. 1. Handler ...
- require.context的妙用
比较好用,记录下来. 以下方法将获取vuex中Modules文件夹里的所有modules并导出. const files = require.context(".", false, ...
- flutter中使用redux之多界面互动
在上一篇文章,我们介绍了如何在flutter中使用redux.在上一篇文章的例子中,我们使用了单界面集成redux,但是在实际项目中,我们通常涉及多个模块,每个模块涉及多个界面,那么如何使用redux ...
- Linux从入门到精通系列之NFS
网络文件系统(NFS,Network File System)是一种将远程主机上的分区(目录)经网络挂载到本地系统的一种机制,通过对网络文件系统的支持,用户可以在本地系统上像操作本地分区一样来对远程主 ...
- #Week7 Neural Networks : Learning
一.Cost Function and Backpropagation 神经网络的损失函数: \[J(\Theta) = - \frac{1}{m} \sum_{i=1}^m \sum_{k=1}^K ...
- Axios 拦截器中添加headers 属性
描述: 已在网上查过怎么在 interceptors 中对header进行处理,// http request 拦截器 axios.interceptors.request.use( config = ...
- 7) 项目准备流程 和 django权限六表
一.项目准备 1. 创建django项目 2. 创建数据库 —— init文件中声明mysql —— settings中配置数据库 import pymysql pymysql.install_as_ ...
- 安装KubeSphere
1. KubeSphere 是什么 1.1. 官方解释 KubeSphere是一个分布式操作系统,提供以Kubernetes为核心的云原生堆栈,旨在成为第三方应用程序的即插即用架构,以促进其生态系统的 ...
- 过滤idea一些不需要的文件和文件夹的显示,在使用svn的时候可以很方便的过滤不需要提交的文件
*.classpath;*.gitignore;*.hprof;*.idea;*.iml;*.lst;*.project;*.pyc;*.pyo;*.rbc;*.settings;*.sh;*.yar ...