当前位置:网站首页>A great thinking problem cf1671d insert a progression
A great thinking problem cf1671d insert a progression
2022-07-28 00:11:00 【ZaneBobo】

One . The question :
Here's a length of n Array of , Here are some numbers for you 1~x, Ask you to insert these numbers into the array ( It can be inserted at both ends of the array , It can also be inserted into the array ), Make the sum of the absolute values of the differences between every two adjacent elements of the whole array minimum ( In fact, each element in the array is treated as a column of corresponding height , This value is the total number of steps from the first column to the last column , This value is hereinafter referred to as sum) Please find a way to insert , Then output the minimum sum.
Two . Ideas :
If the element range of the existing array is l~r that ,l~r The insertion position can be found in the array , And do not change the original array sum,( If you want to insert x, As long as there is a position pos One element on both sides of this position is less than x One is greater than x that will do , This will not increase sum, Because this is actually a step paved between the two positions , I didn't go up or down more ), that 1~l-1, and r+1~x What about these numbers ? In fact, we can 1~l-1 These figures are regarded as 1 That's it , Because all the elements in the original array must be better than 1 Big , So no matter 1 where , There will be a downhill road and an uphill road to go , In fact, it increases the number of steps (sum), And lay in between “ steps ” It doesn't increase sum, For example, now there are two numbers 2 3 We put 1 Insert it 4 1 5 Now 21 It's downhill ,1 3 It's uphill , Originally from 2 To 3 Just go 1 Step , Now go more 6 Step , And if the Take the rest 2 3 Insert it as a step , Then it will become 4 3 2 1 5 So follow the steps to 1 It will not increase any steps , perhaps 4 1 2 3 5 Follow the steps to 5 It won't increase any steps , So just think about 1 The position of , At this point, we iterate through each put 1 The location of , Then calculate how much it will increase sum that will do .r+1~x Empathy , Think as a whole x that will do .
3、 ... and . Code
#include<iostream>
#include<algorithm>
using namespace std;
const int N=2E5+10;
int a[N];
typedef long long LL;
void solve()
{
int n,x;
cin>>n>>x;
int maxx=0;
int minn=0x3f3f3f3f;
LL ans=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=1;i<=n;i++)
{
if(i!=n)
ans+=max(a[i+1]-a[i],a[i]-a[i+1]);// Of the initial array sum
maxx=max(maxx,a[i]);
minn=min(minn,a[i]);
}
if(maxx<x)
{
int minw=0x3f3f3f3f;
for(int i=1;i<n;i++)
{
minw=min(minw,x-a[i]+x-a[i+1]-abs(a[i]-a[i+1]));// Here is the minimum value of the increase. Don't forget to subtract the original steps
}
minw=min(minw,min(x-a[1],x-a[n]));
ans+=minw;
}
if(minn>1)
{
int minw=0x3f3f3f3f;
for(int i=1;i<n;i++)
{
minw=min(minw,a[i]-1+a[i+1]-1-abs(a[i]-a[i+1]));
}
minw=min(minw,min(a[1]-1,a[n]-1));
ans+=minw;
}
cout<<ans<<endl;
}
int main()
{
int T;
cin>>T;
while(T--)
{
solve();
}
}边栏推荐
- R语言使用hexSticker包将ggplot2包可视化的结果转换为六角图(六角贴、六角形贴纸、ggplot2 plot to hex sticker)
- [Development Tutorial 9] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - heart rate monitoring
- require、loadfile、dofile、load、loadstring
- 网站被黑后处理方法及删除批量恶意代码的方法步骤
- 传奇服中怎么刷装备
- 阿金的思考
- What are the software operation and maintenance monitoring?
- BUUCTF-Dangerous RSA
- XSS Payload 学习浏览器解码
- Monologue of a software Investor: why don't I pursue fast-growing companies
猜你喜欢

Unity 实现简单画板画画功能(笔记)

Is it really hard to understand? What level of cache is the recyclerview caching mechanism?

(12) 51 Single Chip Microcomputer -- use DS18B20 to measure the outdoor temperature in Gongjiang West

给网站套上Cloudflare(以腾讯云为例)

QT with OpenGL (shadow mapping)

Realize today's news website based on native JS
![[roarctf2019] babyrsa Wilson theorem](/img/c1/52e79b6e40390374d48783725311ba.gif)
[roarctf2019] babyrsa Wilson theorem
Edit the copy and paste judgment problem (bug?), WYSIWYG display symbol problem feedback.

(十二)51单片机----用DS18B20浅测一下工(江)西的室外温度

New media content output method - short video
随机推荐
传奇服中怎么刷装备
Redefine analysis - release of eventbridge real-time event analysis platform
Reduce error demonstration
[MRCTF2020]babyRSA
NDK series (6): let's talk about the way and time to register JNI functions
2022/7/24-7/25
[gwctf 2019] boring lottery
Unity implements simple Sketchpad drawing function (notes)
NPM related information
Zcmu--1720: death is like the wind, I want to pretend to force
The first activity of togaf10 standard reading club was successfully held, and the wonderful moments were reviewed!
Assertion mechanism in test class
TCP的粘包拆包问题+解决方案
资深如何确定软件测试结束的标准
J9数字科普:Sui网络的双共识是如何工作的?
BUUCTF-Dangerous RSA
TCP sticking and unpacking problem + Solution
Error:svn: E155010: ‘/Users/.../Desktop/wrokspace/xxx‘ is scheduled for addition, but is missing
Legendary server: what must be modified when the GOM geem2 engine is updated?
泵站远程监控