Codeforces C. Circle of Monsters (思维贪心 / 前缀和)

传送门

题意: 有n个怪兽围成一圈,第i个怪兽都有一个a[i]的生命值和一个b[i]的危害值。你只能选一个起点开始按准时针挨个击杀它们,每次击打伤害值为1。当某个怪兽的生命值<=0时就会爆炸对下一个怪兽产生b[i]的伤害,依次发生连锁反应。试问将所有怪兽击毙至少需要多少次攻击。
在这里插入图片描述
思路:

  • 根据题意可知,只有挨着挨着按顺序击杀怪兽,而击杀所有怪兽的基本差值和是一定的,攻击次数主要取决于起点的选择。
  • 所有我们对起点进行枚举就好,记得在枚举是要先减去之前该点的差值再加该点的怪兽生命值a[i]。

代码实现:

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {
    
    {
    
    1, 0}, {
    
    -1, 0}, {
    
    0, 1}, {
    
    0, -1}};
using namespace std;
const int  inf = 0x3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 1e6 + 5;

int t, n, a[N], b[N], c[N];

signed main()
{
    
    
    IOS;

    cin >> t;
    while(t --){
    
    
        cin >> n;
        for(int i = 1; i <= n; i ++) cin >> a[i] >> b[i];
        int sum = 0;
        for(int i = 1; i <= n; i ++){
    
    
            if(i==1) c[i] = max(a[1]-b[n], 0LL);
            else  c[i] = max(a[i]-b[i-1], 0LL);
            sum += c[i];
        }
        int ans = 1e18;
        for(int i = 1; i <= n; i ++) ans = min(ans, sum-c[i]+a[i]);
        cout << ans << endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Satur9/article/details/109500491