欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

codeforces C. Circle of Monsters

程序员文章站 2022-06-27 11:52:16
...

codeforces C. Circle of Monsters

题目

题意:

给你一个环,环中有nn个小怪兽,然后你有若干子弹,当你杀死一只怪兽的时候,它会对下一个造成bib_i的伤害,每一只怪兽有aia_i的血,

思路:

我们可以把设置一个cic_i就是前一个怪兽死后,这只怪兽剩下的血量,如果不等于00的话,那么这一部分使肯定要用子弹取打的,所以要加上,最后得到一个sumsum,但是这样算还缺了一部分,那就是刚开始的第一只怪兽必须用子弹去打,所以我们只要加上出min(aici)min(a_i-c_i)

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return ;
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1:1;
    ret = (c == '-') ? 0:(c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return ;
}
inline void out(int x) {
    if (x > 9) out(x / 10);
    putchar(x % 10 + '0');
}
const int maxn = 3e+5 + 10;
ll a[maxn] = {0}, b[maxn] = {0}, c[maxn] = {0};
int main() {
    int t, n;
    read(t);
    while (t--) {
        ll sum = 0;
        read(n);
        for (int i = 1; i <= n; i++) {
            read(a[i]), read(b[i]);
            if (i > 1) {
                c[i] = max(0ll, a[i] - b[i - 1]);
                sum += c[i];
            }
        }
        c[1] = max(a[1] - b[n], 0ll);
        sum += c[1];
        ll Min = 1e13;
        for (int i = 1; i <= n; i++) {
            Min = min(Min, a[i] - c[i]);
        }
        sum += Min;
        printf("%lld\n", sum);
    }
    return 0;
}

相关标签: codeforces