当前位置:网站首页>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;
}
边栏推荐
- Plus版SBOM:流水线物料清单PBOM
- 孤独的年轻人,戒不了Jellycat
- Simple blockchain day based on bolt database (2)
- Strictly control outdoor operation time! Foshan housing and Urban Rural Development Bureau issued a document: strengthening construction safety management during high temperature
- Principle of control system based on feedback rate
- [网摘][医学影像] 常用的DICOM缩略图解释以及Viewer converter 转换工具
- Difference quotient approximation of wechat quotient
- Idea: can't use subversion command line client: SVN solution
- Sword finger offer notes: t58 - I. flip word order
- Binary search decision tree (average search length of binary search tree)
猜你喜欢

我在英国TikTok做直播电商

How to make a graph? Multiple subgraphs in a graph are histogram (or other graphs)

Shell脚本文本三剑客之awk

Watermelon Book + pumpkin book chapter 1-2

【数据库数据恢复】SQL Server数据库所在磁盘分区空间不足报错的数据恢复案例

I do live e-commerce in tiktok, UK

5V升压9V芯片
Do you really understand the underlying data structure skip list of Zset in redis?

SMA TE: Semi-Supervised Spatio-Temporal RepresentationLearning on Multivariate Time Series

Shutter project scrollcontroller attached to multiple scroll views, failed assertion: line 109 POS 12 error handling
随机推荐
In the first half of the year, the number of fires decreased by 27.7%. Guangdong will improve the fire safety quality of the whole people in this way
Sword finger offer note: t45. arrange the array into the smallest number
V-show失效问题
查看系统下各个进程打开的文件描述符数量
Unity shader - Laser special effect shader[easy to understand]
Idea: can't use subversion command line client: SVN solution
Check the number of file descriptors opened by each process under the system
STS下载教程(include官网无法下载解决方案)
Sword finger offer notes: t57 - ii Continuous positive sequence with sum s
Difference quotient approximation of wechat quotient
解决方案:Can not issue executeUpdate() or executeLargeUpdate() for SELECTs
Sword finger offer notes: t58 - I. flip word order
[网摘][医学影像] 常用的DICOM缩略图解释以及Viewer converter 转换工具
Go Beginner (4)
解决@OneToMany查询陷入循环引用问题
Bishi journey
Write and read system temporary files: createtempfile and tempfilecontent[easy to understand]
TapNet: Multivariate Time Series Classification with Attentional Prototypical Network
JS字符串方法总结
You haven't connected to the proxy server. There may be a problem or the address is incorrect (how to check the proxy server IP)