Educational Codeforces Round 121 (Rated for Div. 2)-C. Monsters And Spells

题目链接
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 n 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 t (1≤t≤104) — the number of testcases.

The first line of the testcase contains a single integer n (1≤n≤100) — the number of monsters in the level.

The second line of the testcase contains n integers k1<k2<⋯<kn (1≤ki≤109) — 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 n integers h1,h2,…,hn (1≤hi≤ki≤109) — the health of the i-th monster.

The sum of n over all testcases doesn’t exceed 104.

Output
For each testcase, print a single integer — the least amount of mana required for Monocarp to kill all monsters.

Example
input
3
1
6
4
2
4 5
2 2
3
5 7 9
2 1 2
output
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.

题意:在一条线上有n个怪物,每个怪物需要对应的法力去消灭,选定一个地方开始蓄力,位置每加1相应的法力值也加1,你需要确定消灭每一个怪物至少需要从哪一个点开始蓄力,尽可能使过程中的法力值相加的和最小
思路:从后往前,因为后方可能会影响到前方的,当不影响前方的时候,将当前的区间存入vector,如果影响前方,则更新当前的区间左端点,最后通过等差数列求和即可

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll MOD=998244353;
const ll N=505;
const ll INF=0x3f3f3f;
void init()
{

}
void slove()
{
    ll n;cin>>n;
    ll a[n+5];
    ll b[n+5];
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=0;i<n;i++) cin>>b[i];
    vector<pair<ll,ll> >v;
    ll right=a[n-1];
    ll left=a[n-1]-b[n-1]+1;
    for(int i=n-2;i>=0;i--)
    {
        ll r=a[i];
        ll l=a[i]-b[i]+1;
        if(left>r){
            v.push_back({right,left});
            right=r;
            left=l;
        }else{
            left=min(left,l);
        }
    }
    v.push_back({right,left});
    ll ans=0;
    for(int i=0;i<v.size();i++)
    {
        ans+=((2+v[i].first-v[i].second)*(v[i].first-v[i].second+1)/2);
        //cout<<v[i].first<<' '<<v[i].second<<' '<<ans<<endl;
    }
    cout<<ans<<'\n';
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //init();

    int t;cin>>t;
    while(t--)
        slove();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值