Building Permutation

CodeForces - 285C

Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.

You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.

Input

The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).

Output

Print a single number — the minimum number of moves.

Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.

Examples

Input
2
3 0
Output
2
Input
3
-1 -1 2
Output
6

Note

In the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).

In the second sample you need 6 moves to build permutation (1, 3, 2).

sol:较显然的是最小的变成1,次小的变成2,以此类推,应该是最优的

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n;
ll a[N];
int main()
{
int i;
ll ans=;
R(n);
for(i=;i<=n;i++) R(a[i]);
sort(a+,a+n+);
for(i=;i<=n;i++) ans+=abs(i-a[i]);
Wl(ans);
return ;
}
/*
Input
2
3 0
Output
2 Input
3
-1 -1 2
Output
6
*/

codeforces285C的更多相关文章

随机推荐

  1. Java多线程(九)—— interrupt()和线程终止方式

    一.interrupt() 说明 interrupt()的作用是中断本线程.本线程中断自己是被允许的:其它线程调用本线程的interrupt()方法时,会通过checkAccess()检查权限.这有可 ...

  2. Linux下配置mysql远程访问

    1 编辑mysql的配置文件 mysqld.cnf root@iZwz99xkrnh5xye3zgi4btZ:~# vi /etc/mysql/mysql.conf.d/mysqld.cnf 2 把  ...

  3. Eclipse 设置保存代码时自动格式化

    在码代码或者优化的时候,经常需要使用到ctrl+shift+F来格式化代码,但其实ecilpse已经自带自动格式化功能了,只是没有默认开启. 正确的打开方式:windows-->Preferen ...

  4. 蓝牙SDP协议概述

    之前写了一篇 bluedroid对于sdp的实现的源码分析   ,他其实对于sdp 协议本身的分析并不多,而是侧重于 sdp 处于Android bluedroid 架构中的代码流程,这篇文章,是针对 ...

  5. [C# ASP.NET]如何让IIS Express支持外部(局域网)连接

    声明:本文为www.cnc6.cn原创,转载时请注明出处,谢谢! 一.搭建环境: 1.系统:Win10 1809 2.IDE:Visual Studio 2017 3.Framework: 4.6.1 ...

  6. 跨语言调用Hangfire定时作业服务

    跨语言调用Hangfire定时作业服务 背景 Hangfire允许您以非常简单但可靠的方式执行后台定时任务的工作.内置对任务的可视化操作.非常方便. 但令人遗憾的是普遍都是业务代码和hagnfire服 ...

  7. win10 再次重装系统

    去年经历了一次硬盘损坏,一蹶不振,伤了元气, 生产环境的系统一直没有好好的维护,我个人也是,有时一闪而过的窗口总让我觉得有什么不对,现在终于出现问题,XNA项目突然无法编译 提示: 严重性 代码 说明 ...

  8. Django signals 信号作用及用法说明

    参考:https://docs.djangoproject.com/en/1.11/ref/signals/ 1.Model signals django.db.models.signales 作用于 ...

  9. 【内存溢出】Maven编译时内存溢出的问题解决方式

    原文地址:https://www.cnblogs.com/sunny3096/p/7524635.html 编译源码时报出java.lang.OutOfMemoryError: Java heap s ...

  10. Linux查看端口

    1.lsof -i:端口号 用于查看某一端口的占用情况,比如查看8000端口使用情况,lsof -i:8000   2.netstat -tunlp |grep 端口号 用于查看指定的端口号的进程情况 ...