【Codeforces 1334C】Circle of Monsters | 思维、枚举

本文介绍了一道关于环状怪物的算法题,玩家需找出消灭所有怪物所需的最小攻击次数。文章提供了一种优化的解决方案,避免了直接模拟的高时间复杂度,并附带了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目大意:

n n n个怪物,第 i i i个怪物有 a i a_i ai生命值,你可以每次对一个怪物造成 1 1 1点伤害,当第 i i i个怪物死去后,会对 i + 1 i+1 i+1个怪物产生 b i b_i bi伤害(如果 i = n , i + 1 = 1 i = n,i+1 = 1 i=n,i+1=1),询问最少需要攻击多少次使得所有怪物全部死亡?

题目思路:

去年5月份做过的题目,我却看不懂自己之前怎么写的了…

然后就有了一个和去年5月完全不一致的写法,可能是因为自己变弱了。

首先考虑因为是一个环,所以一旦引爆一个后,后面肯定会发生连锁反应。

所以,有一个思路就非常的简单,枚举炸哪个, n 2 n^2 n2去模拟每个位置的花费,求最小花费就可以了。

但是复杂度不允许,所以此时需要加一点点思维:

预处理数组 c [ i ] c[i] c[i] c [ i ] c[i] c[i]表示第i个物品通过第 i − 1 i-1 i1个物品引爆后,还需要攻击几次。

c [ i ] c[i] c[i]的和为 s u m sum sum,然后枚举每一个位置时,只需要求:

m i n i = 1 i = n { s u m − c [ i ] + a [ i ] } min_{i=1}^{i=n}\{sum-c[i]+a[i]\} mini=1i=n{sumc[i]+a[i]}

Code:

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<string.h>
#include<iostream>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const ll INF= 1e18+7;
const ll maxn = 1e6+700;
const int M = 1e6+8;
const ll mod= 998244353;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a){char c=getchar();T x=0,f=1;while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+c-'0';c=getchar();}a=f*x;}
ll n,m,p;
ll a[maxn],b[maxn];
ll c[maxn];
int main(){
  int T;scanf("%d",&T);
  while(T--){
    read(n);
    for(int i=1;i<=n;i++){
      read(a[i]);
      read(b[i]);
    }
    a[0] = a[n];
    b[0] = b[n];
    ll tmp = 0;
    for(int i=1;i<=n;i++){
      if(b[i-1]>=a[i]) c[i] = 0;
      else c[i] = a[i] - b[i-1];
      tmp += c[i];
    }
    ll minl = INF;
    for(int i=1;i<=n;i++)
      minl = min(minl,tmp-c[i]+a[i]);
    dl(minl);
  }
  return 0;
}
/***

***/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只酷酷光儿( CoolGuang)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值