Circle of Monsters

Circle of Monsters

Circle of Monsters 题目链接

思路

先是把所有伤害互传造成的结果记录,然后再通过简单的枚举初始位置来找最小值,整体复杂度是 \(O(n)\) 的,可以说是一道水题,主要思想应该就是贪心吧。

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e6 + 10;
ll a[N], b[N], cost[N];
int n;
int main() {
    // freopen("in.txt", "r", stdin);
    int t;
    scanf("%d", &t);
    while(t--) {
        ll sum = 0;
        scanf("%d", &n);
        for(int i = 0; i < n; i++) {
            scanf("%lld %lld", &a[i], &b[i]);//注意cost[i]不可能小于0所以这里要特判一下。
            if(i)   cost[i] = max((ll)0, a[i] - b[i - 1]), sum += cost[i];
        }
        cost[0] = max((ll)0, a[0] - b[n - 1]);
        sum += cost[0];
        ll ans = sum - cost[0] + a[0];//取位置0作为起始位置,去找最优的起始位置。
        for(int i = 0; i < n; i++)
            ans = min(ans, sum - cost[i] + a[i]);
        printf("%lld\n", ans);
    }
    return 0;
}
posted @ 2020-04-29 15:52  lifehappiness  阅读(54)  评论(0编辑  收藏  举报