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

2024-01-11 05:32

本文主要是介绍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;
}

这篇关于Educational Codeforces Round 121 (Rated for Div. 2)-C. Monsters And Spells的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/593315

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

CSS实现DIV三角形

本文内容收集来自网络 #triangle-up {width: 0;height: 0;border-left: 50px solid transparent;border-right: 50px solid transparent;border-bottom: 100px solid red;} #triangle-down {width: 0;height: 0;bor

创建一个大的DIV,里面的包含两个DIV是可以自由移动

创建一个大的DIV,里面的包含两个DIV是可以自由移动 <body>         <div style="position: relative; background:#DDF8CF;line-height: 50px"> <div style="text-align: center; width: 100%;padding-top: 0px;"><h3>定&nbsp;位&nbsp;

Codeforces Round 971 (Div. 4) (A~G1)

A、B题太简单,不做解释 C 对于 x y 两个方向,每一个方向至少需要 x / k 向上取整的步数,取最大值。 由于 x 方向先移动,假如 x 方向需要的步数多于 y 方向的步数,那么最后 y 方向的那一步就不需要了,答案减 1 代码 #include <iostream>#include <algorithm>#include <vector>#include <string>

CF#271 (Div. 2) D.(dp)

D. Flowers time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/474/problem/D We s

CF #278 (Div. 2) B.(暴力枚举+推导公式+数学构造)

B. Candy Boxes time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/488/problem/B There