当前位置:网站首页>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)
总结
绝对值的性质有点意思
边栏推荐
- Idea远程提交spark任务到yarn集群
- 看抖音直播Beyond演唱会有感
- [Chongqing Guangdong education] Chongqing Engineering Vocational and Technical College
- Extension and application of timestamp
- [EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)
- Natural language processing (NLP) - third party Library (Toolkit):allenlp [library for building various NLP models; based on pytorch]
- PHP determines whether an array contains the value of another array
- XML Configuration File
- 数据分析思维分析方法和业务知识——分析方法(三)
- Classic CTF topic about FTP protocol
猜你喜欢
[groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)
The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea
LeetCode 1598. Folder operation log collector
猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
Browser reflow and redraw
如何利用Flutter框架开发运行小程序
Uniapp development, packaged as H5 and deployed to the server
【EI会议分享】2022年第三届智能制造与自动化前沿国际会议(CFIMA 2022)
[EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)
Go learning --- structure to map[string]interface{}
随机推荐
[Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
Comment faire votre propre robot
Yolov5, pychar, Anaconda environment installation
Extracting profile data from profile measurement
Promise
Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
The relationship between FPGA internal hardware structure and code
小程序容器可以发挥的价值
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
The value of applet containers
《编程之美》读书笔记
LeetCode 1189. Maximum number of "balloons"
How to use the flutter framework to develop and run small programs
2022-02-13 work record -- PHP parsing rich text
Common API classes and exception systems
【EI会议分享】2022年第三届智能制造与自动化前沿国际会议(CFIMA 2022)
Pointer - character pointer
SQLServer连接数据库读取中文乱码问题解决
详细页返回列表保留原来滚动条所在位置
[EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)