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. 洛谷 P2802 回家

    题目链接 https://www.luogu.org/problemnew/show/P2802 题目描述 小H在一个划分成了n*m个方格的长方形封锁线上. 每次他能向上下左右四个方向移动一格(当然小 ...

  2. Charles抓包显示乱码解决方法

    [问题现象] 在抓https协议请求时,Request和Response显示乱码了: [解决办法] 第一步:点击 [工具栏-->Proxy-->SSL Proxying Settings. ...

  3. A2D JS框架 - AOP封装

    AOP在js中的实现,先看看用法吧: var A2D = $.noConflict();//不要误会,此乃我自己写的A2D框架,非jQuery function fn1(name, age) { co ...

  4. Java 多线程(六)之Java内存模型

    目录 1. 并发编程的两个问题 2 CPU 缓存模型 2.1 CPU 和 主存 2.2 CPU Cache 2.3 CPU如何通过 Cache 与 主内存交互 2.4 CPU 缓存一致性问题 3 Ja ...

  5. developer的996,需要谁来拯救

    不为996辩护,但向奋斗者致敬! 随着996.icu愈演愈烈,不仅是国际友人发文问候,连国内互联网的大佬都被卷进风波,整理下大致思路如下: 马云:因为有自己想要实现的目标,因为有奔头,所以我们努力工作 ...

  6. 使用protostuff自定义编解码器优化springcloud-feign性能

    前言 Spring Cloud feign是伪RPC方式解决微服务间的调用.翻看FeignCloudFeign源码,可以看到Feign默认使用HttpUrlConnection; 代码在Default ...

  7. Unique Snowflakes UVA - 11572 (离散化+尺取法)

    Emily the entrepreneur has a cool business idea: packaging and selling snowflakes. She has devised a ...

  8. Python集合及其运算

    目录 集合(set) 集合的创建 集合的操作 集合的运算 子集与父集 集合(set) 集合是由不同可hash的值组成的,里面所有的值都是唯一的,也是无序的 集合的创建 >>>set_ ...

  9. 消息队列queue

    一.queue 在多线程编程中,程序的解耦往往是一个麻烦的问题,以及在socket网络编程中也会有这样的问题.recv 和send之间,如果服务端有消息,问题需要发送给客户端,而那边的recv 被主程 ...

  10. ubuntu安装chkconfig.deb系统服务管理工具

    chkconfig简介:chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息. 参数用法:   --add 增加所指定的系统服务,让chkconfig指令得以管理它,并同时在系统 ...