题目链接:点击查看
题目大意:给出一个有顺序的环形敌人序列,每个敌人有两个属性,分别记为 a 和 b ,a 为 该敌人的血量,需要相应的子弹数量才能击败,当敌人 i 被击败后,他会爆炸,对第 i + 1 名敌人造成 b[ i ] 的伤害,问至少需要用多少子弹才能将敌人都消灭
题目分析:因为 b 都是大于 0 的数,所以最优解肯定是先挑选一个敌人开始,然后按照顺序依次击杀,不难看出这样是最优的,那么找出第一个击杀的敌人成了这个题的突破口,我的第一反应是找到 a[ i ] 的最小值入手,但不幸的是这样做并不对,后来看到了数据范围给了提示,就恍然大悟了,想一下为什么 a 和 b 的数值都给到了 1e12 ,而不是正常的 1e9 或 1e5 呢,显然是需要进行某种操作,而不能超过 1e18 ,这相差了不到 1e6 的量级恰好就和敌人的数量 n 对应了起来,所以数据范围提示我们需要维护一个前缀和,这样一想我们因为只是第一个敌人的选择不一样,所以可以一层循环枚举起点然后维护最小值作为答案
代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<unordered_map>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int N=3e5+100;
LL a[N],b[N],c[N];
int main()
{
#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);
int w;
cin>>w;
while(w--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lld%lld",a+i,b+i);
LL sum=0;
for(int i=1;i<=n;i++)//敌人受到爆炸后的剩余血量记为数组 c
{
if(i==1)
c[i]=a[1]-b[n];
else
c[i]=a[i]-b[i-1];
if(c[i]<0)
c[i]=0;
sum+=c[i];
}
LL ans=0x3f3f3f3f3f3f3f3f;
for(int i=1;i<=n;i++)
ans=min(ans,sum-c[i]+a[i]);
printf("%lld\n",ans);
}
return 0;
}