当前位置:网站首页>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)
总结
绝对值的性质有点意思
边栏推荐
- State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.
- 建立时间和保持时间的模型分析
- STM32按键消抖——入门状态机思维
- Go learning --- structure to map[string]interface{}
- OpenCV经典100题
- The global and Chinese markets of dial indicator calipers 2022-2028: Research Report on technology, participants, trends, market size and share
- MIT博士论文 | 使用神经符号学习的鲁棒可靠智能系统
- How to make your own robot
- SAP Spartacus home 页面读取 product 数据的请求的 population 逻辑
- [groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)
猜你喜欢

Free chat robot API

OpenCV经典100题
![[EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)](/img/39/9d189a18f3f75110b400506e274391.png)
[EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)

Idea remotely submits spark tasks to the yarn cluster
![[groovy] JSON serialization (jsonbuilder builder | generates JSON string with root node name | generates JSON string without root node name)](/img/dd/bffe27b04d830d70f30df95a82b3d2.jpg)
[groovy] JSON serialization (jsonbuilder builder | generates JSON string with root node name | generates JSON string without root node name)

Set data real-time update during MDK debug

Date类中日期转成指定字符串出现的问题及解决方法

Data analysis thinking analysis methods and business knowledge - analysis methods (III)

Room cannot create an SQLite connection to verify the queries
![[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)
随机推荐
[groovy] compile time metaprogramming (compile time method injection | method injection using buildfromspec, buildfromstring, buildfromcode)
anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败
How to use the flutter framework to develop and run small programs
Browser reflow and redraw
Yolov5、Pycharm、Anaconda环境安装
Anconda download + add Tsinghua +tensorflow installation +no module named 'tensorflow' +kernelrestart: restart failed, kernel restart failed
Date类中日期转成指定字符串出现的问题及解决方法
AtCoder Beginner Contest 254【VP记录】
Room cannot create an SQLite connection to verify the queries
免费的聊天机器人API
How to make your own robot
Calculate sha256 value of data or file based on crypto++
猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
Spark SQL null value, Nan judgment and processing
MCU通过UART实现OTA在线升级流程
esxi的安装和使用
Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
如何制作自己的机器人
【EI会议分享】2022年第三届智能制造与自动化前沿国际会议(CFIMA 2022)
Spark获取DataFrame中列的方式--col,$,column,apply