当前位置:网站首页>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)
总结
绝对值的性质有点意思
边栏推荐
- curlpost-php
- 免费的聊天机器人API
- esxi的安装和使用
- Extension and application of timestamp
- Spark DF adds a column
- Multithreading and high concurrency (8) -- summarize AQS shared lock from countdownlatch (punch in for the third anniversary)
- 猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
- Spark AQE
- Cloud guide DNS, knowledge popularization and classroom notes
- Keepalive component cache does not take effect
猜你喜欢

Spark SQL null value, Nan judgment and processing

I'm interested in watching Tiktok live beyond concert

Notepad++ regular expression replacement string

Model analysis of establishment time and holding time

Keepalive component cache does not take effect

State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.

Arduino hexapod robot

多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)

Free chat robot API

Go learning - dependency injection
随机推荐
《强化学习周刊》第52期:Depth-CUPRL、DistSPECTRL & Double Deep Q-Network
Go learning - dependency injection
SAP Spartacus home 页面读取 product 数据的请求的 population 逻辑
uniapp开发,打包成H5部署到服务器
Comment faire votre propre robot
Spark SQL UDF function
Room cannot create an SQLite connection to verify the queries
STM32 configuration after chip replacement and possible errors
Yolov5, pychar, Anaconda environment installation
LeetCode 1189. Maximum number of "balloons"
小程序容器可以发挥的价值
anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败
Problems and solutions of converting date into specified string in date class
LeetCode 斐波那契序列
LeetCode 6004. Get operands of 0
For a deadline, the IT fellow graduated from Tsinghua suddenly died on the toilet
LeetCode 8. String conversion integer (ATOI)
[EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)
Intranet Security Learning (V) -- domain horizontal: SPN & RDP & Cobalt strike
STM32 key chattering elimination - entry state machine thinking