codeforces 285C Building Permutation (简单贪心)
来源:互联网 发布:ubuntu安装深度商店 编辑:程序博客网 时间:2024/06/05 02:35
Permutationp is an ordered set of integersp1, p2, ..., pn, consisting ofn distinct positive integers, each of them doesn't exceedn. We'll denote thei-th element of permutation p aspi. We'll call numbern the size or the length of permutationp1, 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.
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line containsn integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
Print a single number — the minimum number of moves.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the%I64d specifier.
23 0
2
3-1 -1 2
6
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).
问题:就是给你一组数...然后让它变为自然数数列(不用管顺序)所需要的最小改变...
分析:问题其实就是找每个数和自然数列差的绝对值的和最小...而自然数列是递增数列...所以把题目里给的数列排成递增的,然后算下差的绝对值的和就行...
注意:有个坑是用来记改变的sum值用int的话会越界...所以还是得用long long...其他的都用int就行...注意题目中给的范围...
#include<iostream>#include<algorithm>using namespace std;int main(){int n,a[300005];cin>>n;for(int i=0;i<n;i++){cin>>a[i];}sort(a,a+n);int t=1;long long sum=0;for(int j=0;j<n;j++){sum+=abs(j+1-a[j]);}cout<<sum<<endl;return 0;}
- codeforces 285C Building Permutation (简单贪心)
- codeforces 285C - Building Permutation
- codeforces 285C - Building Permutation
- Codeforces-285C-Building Permutation
- C. Building Permutation
- codeforces C. Diverse Permutation
- 【CODEFORCES】C. Diverse Permutation
- codeforces 482A Diverse Permutation 贪心
- Codeforces 622D Optimal Number Permutation 【贪心】
- Codeforces 500B New Year Permutation [贪心]
- 【CodeForces 500B】【贪心】New Year Permutation
- codeforces 483C.Diverse Permutation
- CodeForces-483C Diverse Permutation
- CodeForces 483C Diverse Permutation
- codeforces 287 C. Lucky Permutation
- codeforces 525C Ilya and Sticks(简单贪心)
- Codeforces 845 C. Two TVs 思路:简单贪心算法
- CodeForces 275C 贪心
- CICS集群安装相关要点记录
- 互联网金融风控措施
- 通过servlet封装不同应用之间的访问方法
- 互联网金融时代下机器学习与大数据风控系统
- C#之构造函数
- codeforces 285C Building Permutation (简单贪心)
- Spring Boot 配置优先级顺序
- 如何解决invalid LOC header错误的问题 - 润乾报表集成
- 创建数据库与查询数据
- Dlib机器学习库系列人脸检测
- IIS http强制转向https(证书已安装好)
- TCPIP三次握手与四次挥手
- 去掉一个最高分,去掉一个最低分以后评委的平均打分。
- jmeter实现根据http请求参数是否有值,选择性的添加参数