1141E - Superhero Battle(数学思维) Codeforces Round #547 (Div. 3)

原题链接:https://codeforces.com/contest/1141/problem/E

题意:超人与怪兽要进行多个个回合的战斗,每一个回合又细分为n分钟,在这一回合中每经过一分钟,怪兽会获得血量的加成或减少,问经过多少分钟后,怪兽第一时间倒地?若不会,输出-1.

解题思路:我们这样想,先遍历一次回合,判断怪兽能否在该回合中倒地,并统计该回合的总收益,若总收益小于等于0,则怪兽永远不可能倒地。之后,再利用取余求出在最后一个回合中h的血量,再遍历该回合,得到倒地的时间。则此题可解。

AC代码:

/*
*邮箱:2825841950@qq.com
*blog:https://blog.csdn.net/hzf0701
*注:代码如有问题请私信我或在评论区留言,谢谢支持。
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<stack>
#include<queue>
#include<cstring>
#include<map>
#include<iterator>
#include<list>
#include<set>
#include<functional>
#include<memory.h>//低版本G++编译器不支持,若使用这种G++编译器此段应注释掉
#include<iomanip>
#include<vector>
#include<cstring>
#define scd(n) scanf("%d",&n)
#define scf(n) scanf("%f",&n)
#define scc(n) scanf("%c",&n)
#define scs(n) scanf("%s",n)
#define prd(n) printf("%d",n)
#define prf(n) printf("%f",n)
#define prc(n) printf("%c",n)
#define prs(n) printf("%s",n)
#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll>  pll;
typedef pair<int, int> pii;
const ll inf = 0x3f3f3f3f;//无穷大
const ll maxn =2e5+2;//最大值。
//*******************************分割线,以上为代码自定义代码模板***************************************//

ll nums[maxn];
ll h;//monster的生命
ll n;//一回合的n分钟。
ll sum;//统计一回合进行的成果。
ll temp;//临时变量。
ll result;
int main(){
	//freopen("in.txt", "r", stdin);//提交的时候要注释掉
	ios::sync_with_stdio(false);//打消iostream中输入输出缓存,节省时间。
	cin.tie(0); cout.tie(0);//可以通过tie(0)(0表示NULL)来解除cin与cout的绑定,进一步加快执行效率。
	cin>>h>>n;
	sum=0;temp=0;
	bool flag=0;
	rep(i,0,n-1){
		cin>>nums[i];
		sum-=nums[i];
		h+=nums[i];
		if(flag==0){
		if(h<=0){
			result=i+1;
			flag=1;
		}
		}
		temp=max(temp,sum);//找到其中消耗最多的血量,便于之后不越界。
	}
	if(flag){
		cout<<result<<endl;
		return 0;
	}
	if(sum<=0){
		cout<<"-1"<<endl;
		return 0;
	}
	temp=(h-temp)/sum;//经过temp个回合,这里减去temp是为了多进行了战斗。
	h-=temp*sum;  //减去temp个回合所消耗的量。
	result=temp*n+n;//前面也进行了一次遍历,算一个回合,需要加上前面的一个回合。
	for(int i=0;;i++){
		h+=nums[i%n];
		result++;
		if(h<=0){
			cout<<result<<endl;
			break;
		}
	}
	return 0;
}

posted @   unique_pursuit  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
点击右上角即可分享
微信分享提示