当前位置:网站首页>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)
总结
绝对值的性质有点意思
边栏推荐
- [groovy] JSON string deserialization (use jsonslurper to deserialize JSON strings | construct related classes according to the map set)
- Free chat robot API
- Curlpost PHP
- KDD 2022 | 脑电AI助力癫痫疾病诊断
- Keepalive component cache does not take effect
- Date类中日期转成指定字符串出现的问题及解决方法
- Atcoder beginer contest 254 [VP record]
- Leetcode Fibonacci sequence
- Data analysis thinking analysis methods and business knowledge - analysis methods (III)
- Browser local storage
猜你喜欢
Room cannot create an SQLite connection to verify the queries
Extracting profile data from profile measurement
Room cannot create an SQLite connection to verify the queries
Idea remotely submits spark tasks to the yarn cluster
[groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)
小程序技术优势与产业互联网相结合的分析
2022-02-13 work record -- PHP parsing rich text
如何制作自己的機器人
[groovy] compile time meta programming (compile time method interception | method interception in myasttransformation visit method)
Model analysis of establishment time and holding time
随机推荐
Leetcode:20220213 week race (less bugs, top 10% 555)
Cloud guide DNS, knowledge popularization and classroom notes
LeetCode 6005. The minimum operand to make an array an alternating array
Intensive learning weekly, issue 52: depth cuprl, distspectrl & double deep q-network
Go learning --- read INI file
数据分析思维分析方法和业务知识——分析方法(二)
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]
Date类中日期转成指定字符串出现的问题及解决方法
Leetcode 44 Wildcard matching (2022.02.13)
After Luke zettlemoyer, head of meta AI Seattle research | trillion parameters, will the large model continue to grow?
Spark SQL UDF function
Starting from 1.5, build a micro Service Framework - call chain tracking traceid
Uniapp development, packaged as H5 and deployed to the server
Keepalive component cache does not take effect
NLP text processing: lemma [English] [put the deformation of various types of words into one form] [wet- > go; are- > be]
How spark gets columns in dataframe --column, $, column, apply
Idea远程提交spark任务到yarn集群
Promise
[groovy] JSON serialization (jsonbuilder builder | generates JSON string with root node name | generates JSON string without root node name)
Room cannot create an SQLite connection to verify the queries