当前位置:网站首页>E. Breaking the Wall
E. Breaking the Wall
2022-07-06 09:25:00 【是小张张呀 zsy】
原题链接
Monocarp plays “Rage of Empires II: Definitive Edition” — a strategic computer game. Right now he’s planning to attack his opponent in the game, but Monocarp’s forces cannot enter the opponent’s territory since the opponent has built a wall.
The wall consists of n sections, aligned in a row. The i-th section initially has durability ai. If durability of some section becomes 0 or less, this section is considered broken.
To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager — a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals 2 damage to the target section and 1 damage to adjacent sections. In other words, if the onager shoots at the section x, then the durability of the section x decreases by 2, and the durability of the sections x−1 and x+1 (if they exist) decreases by 1 each.
Monocarp can shoot at any sections any number of times, he can even shoot at broken sections.
Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him!
Input
The first line contains one integer n (2≤n≤2⋅105) — the number of sections.
The second line contains the sequence of integers a1,a2,…,an (1≤ai≤106), where ai is the initial durability of the i-th section.
Output
Print one integer — the minimum number of onager shots needed to break at least two sections of the wall.
Examples
inputCopy
5
20 10 30 10 20
outputCopy
10
inputCopy
3
1 8 1
outputCopy
1
inputCopy
6
7 6 6 8 5 8
outputCopy
4
inputCopy
6
14 3 8 10 15 4
outputCopy
4
inputCopy
4
1 100 100 1
outputCopy
2
inputCopy
3
40 10 10
outputCopy
7
Note
In the first example, it is possible to break the 2-nd and the 4-th section in 10 shots, for example, by shooting the third section 10 times. After that, the durabilities become [20,0,10,0,20]. Another way of doing it is firing 5 shots at the 2-nd section, and another 5 shots at the 4-th section. After that, the durabilities become [15,0,20,0,15].
In the second example, it is enough to shoot the 2-nd section once. Then the 1-st and the 3-rd section will be broken.
In the third example, it is enough to shoot the 2-nd section twice (then the durabilities become [5,2,4,8,5,8]), and then shoot the 3-rd section twice (then the durabilities become [5,0,0,6,5,8]). So, four shots are enough to break the 2-nd and the 3-rd section
思路很简单,就是分情况讨论(3种情况,详见代码)为什么比赛时候写不出来呜呜呜呜呜
#include<bits/stdc++.h>
using namespace std;
int const N=2e5+10;
int a[N];
int res=1e9;
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
//击破相邻的
for(int i=1;i<n;i++)
{
res=min(res,max(max((a[i]+1)/2,(a[i+1]+1)/2),(a[i]+a[i+1]+2)/3));
}
//击破该值左右两边的
for(int i=2;i<n;i++)
{
res=min(res,min(a[i+1],a[i-1])+(abs(a[i-1]-a[i+1])+1)/2);
}
sort(a+1,a+1+n);
//击破互不相干的;
res=min(res,(a[1]+1)/2+(a[2]+1)/2);
cout<<res<<endl;
return 0;
}
边栏推荐
- Cost accounting [17]
- csapp shell lab
- JS --- detailed explanation of JS facing objects (VI)
- Es6---es6 content details
- STM32学习记录:LED灯闪烁(寄存器版)
- Research Report on printed circuit board (PCB) connector industry - market status analysis and development prospect forecast
- Want to change jobs? Do you know the seven skills you need to master in the interview software test
- JS --- all basic knowledge of JS (I)
- 动态规划前路径问题
- 毕业才知道IT专业大学生毕业前必做的1010件事
猜你喜欢

Matlab example: two expressions of step function
![[C language] twenty two steps to understand the function stack frame (pressing the stack, passing parameters, returning, bouncing the stack)](/img/3a/aadde60352c42199ba287a6997acfa.jpg)
[C language] twenty two steps to understand the function stack frame (pressing the stack, passing parameters, returning, bouncing the stack)

ucore Lab 1 系统软件启动过程

ucore lab5

Introduction to safety testing

The most detailed postman interface test tutorial in the whole network. An article meets your needs

ucore lab 2

C语言必背代码大全

数据在内存中的存储&载入内存,让程序运行起来
Interview answering skills for software testing
随机推荐
Unpleasant error typeerror: cannot perform 'ROR_‘ with a dtyped [float64] array and scalar of type [bool]
学习记录:USART—串口通讯
JDBC introduction
What are the software testing methods? Show you something different
Cost accounting [22]
Mysql database (I)
Knowledge that you need to know when changing to software testing
动态规划前路径问题
LeetCode#198. raid homes and plunder houses
力扣刷题记录--完全背包问题(一)
LeetCode#268. Missing numbers
FSM和i2c实验报告
学习记录:STM32F103 时钟系统概述工作原理
FSM and I2C experiment report
ucore lab7
LeetCode#204. Count prime
Introduction to safety testing
Interview answering skills for software testing
STM32学习记录:输入捕获应用
Cost accounting [21]