当前位置:网站首页>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
边栏推荐
- [simple implementation of file IO]
- anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败
- 猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
- [groovy] compile time meta programming (compile time method interception | method interception in myasttransformation visit method)
- SAP Spartacus home 页面读取 product 数据的请求的 population 逻辑
- I'm interested in watching Tiktok live beyond concert
- [Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
- KDD 2022 | EEG AI helps diagnose epilepsy
- NLP basic task word segmentation third party Library: ICTCLAS [the third party library with the highest accuracy of Chinese word segmentation] [Chinese Academy of Sciences] [charge]
- 2022-02-13 work record -- PHP parsing rich text
猜你喜欢

Model analysis of establishment time and holding time

Spark AQE

Comment faire votre propre robot
![[groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)](/img/09/9076de099147b2d0696fe979a68ada.jpg)
[groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)

Anconda download + add Tsinghua +tensorflow installation +no module named 'tensorflow' +kernelrestart: restart failed, kernel restart failed

Folding and sinking sand -- weekly record of ETF

看抖音直播Beyond演唱会有感

KDD 2022 | EEG AI helps diagnose epilepsy

数据分析思维分析方法和业务知识——分析方法(二)
![Atcoder beginer contest 258 [competition record]](/img/e4/1d34410f79851a7a81dd8f4a0b54bf.gif)
Atcoder beginer contest 258 [competition record]
随机推荐
Codeforces gr19 D (think more about why the first-hand value range is 100, JLS yyds)
Idea远程提交spark任务到yarn集群
curlpost-php
Multithreading and high concurrency (8) -- summarize AQS shared lock from countdownlatch (punch in for the third anniversary)
Ffmpeg captures RTSP images for image analysis
Natural language processing (NLP) - third party Library (Toolkit):allenlp [library for building various NLP models; based on pytorch]
Starting from 1.5, build a micro Service Framework - call chain tracking traceid
Intranet Security Learning (V) -- domain horizontal: SPN & RDP & Cobalt strike
Novice entry depth learning | 3-6: optimizer optimizers
China Taiwan strategy - Chapter 8: digital marketing assisted by China Taiwan
新手入门深度学习 | 3-6:优化器optimizers
Extracting profile data from profile measurement
程序员成长第九篇:真实项目中的注意事项
[groovy] XML serialization (use markupbuilder to generate XML data | create sub tags under tag closures | use markupbuilderhelper to add XML comments)
How to use the flutter framework to develop and run small programs
建立时间和保持时间的模型分析
Go learning --- structure to map[string]interface{}
Extension and application of timestamp
State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.
Search (DFS and BFS)