Educational Codeforces Round 85 (Rated for Div. 2), problem: (C) Circle of Monsters(思维)

链接:https://codeforces.com/contest/1334/problem/C

题意;每个怪有生命值和爆炸值ai,bi,打中一次生命值减少1,它死后会对它的下一个怪产生bi个伤害,问杀死所有怪的最小花费;

花费最小值杀死所有的怪:先将相邻的两个怪的生命值降在爆炸范围内,之后找一个生命值最小的怪,杀死它就完了;

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+10;
typedef long long ll;
ll a[maxn],b[maxn],cost[maxn];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        ll sum=0,mi=2e18;
        for(int i=1; i<=n; i++)
        {
            scanf("%lld%lld",&a[i],&b[i]);
            if(i!=1)
                cost[i]=max((ll)0,a[i]-b[i-1]),sum+=cost[i];
        }
        cost[1]=max((ll)0,a[1]-b[n]);
        sum+=cost[1];
        ll ans=a[1]-cost[1];
        for(int i=1; i<=n; i++)
            ans=min(ans,a[i]-cost[i]);
        printf("%lld\n",sum+ans);
    }
return 0;

}

 

posted on 2020-04-12 16:31  mmn  阅读(141)  评论(0编辑  收藏  举报