Circle of Monsters CodeForces - 1334C(前缀和+贪心)

You are playing another computer game, and now you have to slay n monsters. These monsters are standing in a circle, numbered clockwise from 1 to n. Initially, the i-th monster has ai health.

You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by 1 (deals 1 damage to it). Furthermore, when the health of some monster i becomes 0 or less than 0, it dies and explodes, dealing bi damage to the next monster (monster i+1, if i<n, or monster 1, if i=n). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.

You have to calculate the minimum number of bullets you have to fire to kill all n monsters in the circle.

Input
The first line contains one integer T (1≤T≤150000) — the number of test cases.

Then the test cases follow, each test case begins with a line containing one integer n (2≤n≤300000) — the number of monsters. Then n lines follow, each containing two integers ai and bi (1≤ai,bi≤1012) — the parameters of the i-th monster in the circle.

It is guaranteed that the total number of monsters in all test cases does not exceed 300000.

Output
For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters.

Example
Input
1
3
7 15
2 14
5 3
Output
6
思路:我们可以计算出每一个怪物,如果靠前一个怪物引爆,还需要的补的枪数计算出来。然后将这些数加起来。接下来我们要做的就是枚举怪物,这里代表的是第一个需要开枪杀死的怪物,然后更新答案最小值。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=3e5+100;
ll a[maxx],b[maxx],c[maxx];
int n;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		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++)
		{
			if(i==1) c[i]=max(0ll,a[i]-b[n]);
			else c[i]=max(0ll,a[i]-b[i-1]);
			sum+=c[i];
		}
		ll ans=1e18;
		for(int i=1;i<=n;i++) ans=min(ans,sum-c[i]+a[i]);
		cout<<ans<<endl;
	}
	return 0;
}

努力加油a啊,(o)/~

发布了672 篇原创文章 · 获赞 127 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/105604262