codeforces285C
Building Permutation
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
2
3 0
2
3
-1 -1 2
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的更多相关文章
随机推荐
- Wireshark抓包分析TCP 3次握手、4次挥手过程
Wireshark简介 更多有关Wireshark的教程.软件下载等,请见:http://www.52im.net/thread-259-1-1.html,本文只作简要介绍. 1Wireshark 是 ...
- bat性能效率受啥影响
代码效率的提升往往由算法决定,曾发过专贴(浅谈提高代码效率的编写习惯:http://tieba.baidu.com/p/1187281687),但是以实例为主,并没有太多的文字说明,现在归纳一下:影响 ...
- Python爬虫 获得淘宝商品评论
自从写了第一个sina爬虫,便一发不可收拾.进入淘宝评论爬虫正题: 在做这个的时候,也没有深思到底爬取商品评论有什么用,后来,爬下来了数据.觉得这些数据可以用于帮助分析商品的评论,从而为用户选择商品提 ...
- mysql查询表结果排序
要求:将数据表tab1中DeviceName为‘XI003’,'DO107'的数据放在查询结果的前面,同时对其他查询结果进行降序排序 sql语句: SELECT DISTINCT DeviceName ...
- Java多线程学习(二)---线程创建方式
线程创建方式 摘要: 1. 通过继承Thread类来创建并启动多线程的方式 2. 通过实现Runnable接口来创建并启动线程的方式 3. 通过实现Callable接口来创建并启动线程的方式 4. 总 ...
- .NetCore 资料分享
.NetCore3.0 你还不打算入手么? 这次主要是推荐一些自己学习中遇到的一些好的资料和自己的看法( 我推荐的都是我看过的 Asp.Net Core 不做介绍了,直接分享资料了 资料: .NetC ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(90)-EF 扩展操作
上一篇讲了EF直接执行SQL与存储过程的用 法 这次我们来看 EntityFramework-Plus(免费开源) 库的用法相比其他扩展库,这个更加新并且用法更加简单 这是一个对Entity Fram ...
- Python-初识算法-51
# 什么叫算法# 计算的方法 : 人脑复杂 计算机简单 # 99 * 13 = 1287 = 13*100 - 13# 查找 : 找数据# 排序 :# 最短路径 # 我们学习的算法 都是过去时# 了解 ...
- 通过this()调用有参构造方法
使用原因:在通过无参构造方法实例化对象时,如果有属性可以设置默认值,可通过在无参构造方法中使用this()调用有参构造方法实现. this()需要写在无参构造方法的第一行! 例子:在没有给出小猫的名字 ...
- 最短路DAG
边权皆为正时,有最短路DAG. 最短路DAG代表了从原点到每个点的所有最短路. 最短路树个数=最短路DAG生成树个数.用DAG生成树计数即可.复杂度\(O(n+m)\).