当前位置:网站首页>cf:D. Insert a Progression【关于数组中的插入 + 绝对值的性质 + 贪心一头一尾最值】
cf:D. Insert a Progression【关于数组中的插入 + 绝对值的性质 + 贪心一头一尾最值】
2022-07-06 00:37:00 【白速龙王的回眸】
分析
解释请看注释
考虑1和x
三个情况即可
ac code
import sys
input = sys.stdin.readline
# we want to insert x into a1 ... an
# if x belongs to [a1, an]
# there must be a period s.t. ai <= x <= aj
# which means the extra effect of x disappear
# therefore we only need to consider 1 and x
# others can insert between 1 and x, just vaporate
# if min <= 1 and x <= max nothing happen
# otherwise, we consider 1 < min
# three cases: abs(1 - a.front()), abs(1 - a.back()), abs(1 - min) * 2
# e.g. minn 1 ai, 1 give extra 2 * (minn - 1)
for _ in range(int(input())):
n, x = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0
for i in range(1, n):
ans += abs(a[i] - a[i - 1])
# for 1 <= min
p = min(abs(1 - a[0]), abs(1 - a[-1]), 2 * abs(1 - min(a)))
# for x
q = 0
if x > max(a):
q = min(abs(x - a[0]), abs(x - a[-1]), 2 * abs(x - max(a)))
ans += p + q
print(ans)
总结
绝对值的性质有点意思
边栏推荐
- Keepalive component cache does not take effect
- 数据分析思维分析方法和业务知识——分析方法(二)
- Spark SQL空值Null,NaN判断和处理
- How to solve the problems caused by the import process of ecology9.0
- Free chat robot API
- NLP text processing: lemma [English] [put the deformation of various types of words into one form] [wet- > go; are- > be]
- An understanding of & array names
- Arduino六足机器人
- How to use the flutter framework to develop and run small programs
- Cloud guide DNS, knowledge popularization and classroom notes
猜你喜欢
Problems and solutions of converting date into specified string in date class
Atcoder beginer contest 258 [competition record]
Spark SQL null value, Nan judgment and processing
AtCoder Beginner Contest 254【VP记录】
MDK debug时设置数据实时更新
数据分析思维分析方法和业务知识——分析方法(二)
电机的简介
Extension and application of timestamp
How to make your own robot
《强化学习周刊》第52期:Depth-CUPRL、DistSPECTRL & Double Deep Q-Network
随机推荐
Browser reflow and redraw
[EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)
I'm interested in watching Tiktok live beyond concert
After Luke zettlemoyer, head of meta AI Seattle research | trillion parameters, will the large model continue to grow?
FPGA内部硬件结构与代码的关系
Synchronized and reentrantlock
State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.
Spark SQL UDF function
MIT博士论文 | 使用神经符号学习的鲁棒可靠智能系统
Lone brave man
Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
Go learning - dependency injection
多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
KDD 2022 | EEG AI helps diagnose epilepsy
可恢复保险丝特性测试
LeetCode 6004. Get operands of 0
The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea
Atcoder beginer contest 258 [competition record]
OpenCV经典100题
Global and Chinese market of water heater expansion tank 2022-2028: Research Report on technology, participants, trends, market size and share