概述
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters.
The level he's currently on contains nnn monsters. The i-th of them appears ki seconds after the start of the level and has hi health points. As an additional constraint, hi≤ki for all 1≤i≤n. All ki are different.
Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level:1,2,3,… The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is 1. Otherwise, let the damage at the previous second be x. Then he can choose the damage to be either x+1 or 1. A spell uses mana: casting a spell with damage x uses x mana. Mana doesn't regenerate.
To kill the i-th monster, Monocarp has to cast a spell with damage at least hi at the exact moment the monster appears, which is ki.
Note that Monocarp can cast the spell even when there is no monster at the current second.
The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters.
It can be shown that it's always possible to kill all monsters under the constraints of the problem.
Input
The first line contains a single integer ttt (1≤t≤10^4) — the number of testcases.
The first line of the testcase contains a single integer nnn (1≤n≤100) — the number of monsters in the level.
The second line of the testcase contains nnn integers k1<k2<⋯<kn (1≤ki≤10^9) — the number of second from the start the i-th monster appears at. All ki are different, ki are provided in the increasing order.
The third line of the testcase contains nnn integers h1,h2,…,hn (1≤hi≤ki≤10^9) — the health of the i-th monster.
The sum of n over all testcases doesn't exceed 10^4.
Output
For each testcase, print a single integer — the least amount of mana required for Monocarp to kill all monsters.
Sample 1
Input | Output |
---|---|
3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 | 10 6 7 |
Note
In the first testcase of the example, Monocarp can cast spells 3,4,5 and 6 seconds from the start with damages 1,2,3 and 4, respectively. The damage dealt at 6 seconds is 4, which is indeed greater than or equal to the health of the monster that appears.
In the second testcase of the example, Monocarp can cast spells 3,4 and 5 seconds from the start with damages 1,2 and 3, respectively.
In the third testcase of the example, Monocarp can cast spells 4,5,7,8 and 9 seconds from the start with damages 1,2,1,1 and 2, respectively.
思路:
题目大概意思就是在第k秒使h健康值的怪兽一击毙命。
就是在[k-h+1,k]区间内等差数列求和。
问题在于区间可能会重合。
处理方式在分情况讨论,如果重合则向前调,前重合之后必定是随前面一个区间走(从何往前看:这个处理方法比较独特,思维不要定式)
特别注意,不要为了偷懒使输入跟处理走。
#include<iostream>
#include<string.h>
using namespace std;
typedef long long ll;
const ll N=1e4;
ll t,ans,m,n;
ll k[N+5],h[N+5];
ll max(ll a,ll b)
{
return a>b?a:b;
}
ll sum(ll a,ll b)
{
return (ll)(a+b)*(b-a+1)/2;
}
int main()
{
scanf("%lld",&t);
while(t--)
{
ans=0;
scanf("%lld",&n);
for(int i=1;i<=n;i++)
cin>>k[i];
for(int i=1;i<=n;i++)
cin>>h[i];
k[0]=0,m=k[n];
for(int i=n;i!=0;i--)
{
if(k[i]-k[i-1]>=h[i])
{
ans+=sum(1,m-k[i]+h[i]);
m=k[i-1];
}
else
{
h[i-1]=max(h[i-1],h[i]-(k[i]-k[i-1]));
}
}
cout<<ans<<endl;
}
return 0;
}
最后
以上就是自然睫毛膏为你收集整理的Monsters And Spells的全部内容,希望文章能够帮你解决Monsters And Spells所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复