当前位置:网站首页>Cf:d. insert a progression [about the insert in the array + the nature of absolute value + greedy top-down]
Cf:d. insert a progression [about the insert in the array + the nature of absolute value + greedy top-down]
2022-07-06 00:44:00 【White speed Dragon King's review】

analysis
Please see the notes for explanation
consider 1 and x
Three situations are sufficient
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)
summary
The nature of absolute value is interesting
边栏推荐
- golang mqtt/stomp/nats/amqp
- Dynamic programming -- linear DP
- China Taiwan strategy - Chapter 8: digital marketing assisted by China Taiwan
- Reading notes of the beauty of programming
- 看抖音直播Beyond演唱会有感
- Arduino hexapod robot
- Promise
- 数据分析思维分析方法和业务知识——分析方法(二)
- STM32 configuration after chip replacement and possible errors
- 常用API类及异常体系
猜你喜欢

Keepalive component cache does not take effect

Cve-2017-11882 reappearance

如何利用Flutter框架开发运行小程序
![[groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)](/img/52/021931181ad3f4bef271b4e98105c2.jpg)
[groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)

Free chat robot API

MDK debug时设置数据实时更新

How to solve the problems caused by the import process of ecology9.0

猿桌派第三季开播在即,打开出海浪潮下的开发者新视野

Classic CTF topic about FTP protocol

Arduino hexapod robot
随机推荐
Keepalive component cache does not take effect
Pointer - character pointer
XML Configuration File
MCU realizes OTA online upgrade process through UART
Codeforces round 804 (Div. 2) [competition record]
LeetCode 1189. Maximum number of "balloons"
Leetcode 450 deleting nodes in a binary search tree
The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea
uniapp开发,打包成H5部署到服务器
[groovy] compile time meta programming (AST syntax tree conversion with annotations | define annotations and use groovyasttransformationclass to indicate ast conversion interface | ast conversion inte
Room cannot create an SQLite connection to verify the queries
Getting started with devkit
Reading notes of the beauty of programming
常用API类及异常体系
如何制作自己的機器人
Study diary: February 13, 2022
An understanding of & array names
NLP text processing: lemma [English] [put the deformation of various types of words into one form] [wet- > go; are- > be]
KDD 2022 | 脑电AI助力癫痫疾病诊断
Set data real-time update during MDK debug