当前位置:网站首页>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)
总结
绝对值的性质有点意思
边栏推荐
- Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
- STM32 key chattering elimination - entry state machine thinking
- 看抖音直播Beyond演唱会有感
- Browser local storage
- 新手入门深度学习 | 3-6:优化器optimizers
- OpenCV经典100题
- MIT doctoral thesis | robust and reliable intelligent system using neural symbol learning
- 云导DNS和知识科普以及课堂笔记
- Model analysis of establishment time and holding time
- Global and Chinese markets for hinged watertight doors 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
SAP Spartacus home 页面读取 product 数据的请求的 population 逻辑
Starting from 1.5, build a micro Service Framework - call chain tracking traceid
State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.
[groovy] JSON string deserialization (use jsonslurper to deserialize JSON strings | construct related classes according to the map set)
[groovy] compile time metaprogramming (compile time method injection | method injection using buildfromspec, buildfromstring, buildfromcode)
Intranet Security Learning (V) -- domain horizontal: SPN & RDP & Cobalt strike
Notepad + + regular expression replace String
数据分析思维分析方法和业务知识——分析方法(三)
Intensive learning weekly, issue 52: depth cuprl, distspectrl & double deep q-network
For a deadline, the IT fellow graduated from Tsinghua suddenly died on the toilet
随机推荐
Free chat robot API
Search (DFS and BFS)
MYSQL GROUP_ The concat function realizes the content merging of the same ID
SAP Spartacus home 页面读取 product 数据的请求的 population 逻辑
《强化学习周刊》第52期:Depth-CUPRL、DistSPECTRL & Double Deep Q-Network
Set data real-time update during MDK debug
LeetCode 1189. Maximum number of "balloons"
[Online gadgets] a collection of online gadgets that will be used in the development process
For a deadline, the IT fellow graduated from Tsinghua suddenly died on the toilet
LeetCode 6005. The minimum operand to make an array an alternating array
Yolov5, pychar, Anaconda environment installation
免费的聊天机器人API
Location based mobile terminal network video exploration app system documents + foreign language translation and original text + guidance records (8 weeks) + PPT + review + project source code
详细页返回列表保留原来滚动条所在位置
KDD 2022 | EEG AI helps diagnose epilepsy
Global and Chinese market of water heater expansion tank 2022-2028: Research Report on technology, participants, trends, market size and share
2022-02-13 work record -- PHP parsing rich text
Keepalive component cache does not take effect
Extracting profile data from profile measurement
Go learning --- structure to map[string]interface{}