当前位置:网站首页>2021-3-23-meituan-regular sequence
2021-3-23-meituan-regular sequence
2022-07-27 12:18:00 【Anethole job】
Title Description :
We call a length n The sequence of is a regular sequence , If and only if the sequence is a sequence composed of 1~n The arrangement of components , That is, the sequence consists of n It's made up of four positive integers , The value is [1,n] Range , And there are no duplicate numbers , At the same time, regular sequences do not require sorting
One day, the small group got a length of n Any sequence of , He needs to operate in a limited number of operations , Turn this sequence into a regular sequence , He can choose a number in the sequence at any time , And add or subtract one to the number .
How many operations can he use at least to turn this sequence into a regular sequence ?
Input description :
The first line of input contains only one positive integer n, Represents the length of any sequence .(1<=n<=20000)
The second line of input contains n It's an integer , Represents the given sequence , The absolute value of each number is less than 10000.
Output description :
The output contains only one integer , Indicates the minimum number of operations .
Example 1
Input
5
-1 2 3 10 100
Output
103
// Greedy Algorithm , We want to minimize the number of steps each number moves ,
// Or let each number find [1,n] Nearest pit filling
// We can sort first , Then press all the numbers [1,n] The pit is filled from small to large
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> vecNums;
int nTemp = 0;
for (int i = 0; i < n; i++)
{
cin >> nTemp;
vecNums.push_back(nTemp);
}
sort(vecNums.begin(), vecNums.end());
int k = 1;
int ans = 0;
for (int i = 0; i < n; i++)
{
ans += abs(k - vecNums[i]);
k++;
}
cout << ans << endl;
return 0;
}
边栏推荐
- Sword finger offer notes: t57 - I. and two numbers of S
- Firewall firewall
- How to make a graph? Multiple subgraphs in a graph are histogram (or other graphs)
- Flash quickly builds an API
- The use of omitempty in go
- Guangdong's finance has taken many measures to help stabilize the "ballast stone" of food security
- One article to understand the index of like in MySQL
- go入门篇 (3)
- Go Beginner (5)
- Solve the problem of @onetomany query falling into circular reference
猜你喜欢

Principle of PWM and generation of PWM wave

Tlc549proteus simulation &sallen key filter &ad736vrms to DC conversion &proteus view 51 register value

Shell编程之正则表达式(Shell脚本文本三剑客之grep)

快抖抢救“失意人”

B 站 713 事故后的多活容灾建设|TakinTalks 大咖分享

Watermelon Book + pumpkin book chapter 1-2

Idea: can't use subversion command line client: SVN solution

TapNet: Multivariate Time Series Classification with Attentional Prototypical Network

Chapter 10 enumeration classes and annotations

STS download tutorial (the solution cannot be downloaded on the include official website)
随机推荐
查看系统下各个进程打开的文件描述符数量
go入门篇 (4)
评价自动化测试优劣的隐性指标
About offline caching application cache / using manifest file caching
N ¨UWA: Visual Synthesis Pre-training for Neural visUal World creAtionChenfei
Introduction to box diagram
One article to understand the index of like in MySQL
严控室外作业时间!佛山住建局发文:加强高温期间建筑施工安全管理
微信小程序必用接口「建议收藏」
Newton Raphson iterative method
Unity shader - Laser special effect shader[easy to understand]
SMA TE: Semi-Supervised Spatio-Temporal RepresentationLearning on Multivariate Time Series
解决@OneToMany查询陷入循环引用问题
Unity Shader 一 激光特效Shader[通俗易懂]
While loop instance in shell
Detailed explanation of flask framework
JVM memory model
Recursive method | Fibonacci sequence
Temporary use of solo, difficult choice of Blog
Guangdong's finance has taken many measures to help stabilize the "ballast stone" of food security