285C - Building Permutation

思路:贪心。因为每个数都不同且不超过n,而且长度也为n,所有排列只能为1 2 3 ......n。所以排好序后与对应元素的差值的绝对值加起来就是答案。

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=3e5+;
int a[N];
int main()
{
int n;
cin>>n;
for(int i=;i<=n;i++)cin>>a[i];
sort(a+,a+n+);
ll ans=;
for(int i=;i<=n;i++)
{
ans+=abs(a[i]-i);
}
cout<<ans<<endl;
return ;
}

Codeforces 285C - Building Permutation的更多相关文章

  1. [Codeforces 1208D]Restore Permutation (树状数组)

    [Codeforces 1208D]Restore Permutation (树状数组) 题面 有一个长度为n的排列a.对于每个元素i,\(s_i\)表示\(\sum_{j=1,a_j<a_i} ...

  2. CodeForces 483C Diverse Permutation

    Diverse Permutation Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64 ...

  3. codeforces 483C.Diverse Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/483/C 题目意思:给出 n 和 k,要求输出一个含有 n 个数的排列 p1, p2, ...,pn,使得 ...

  4. codeforces C. Diverse Permutation

    C. Diverse Permutation time limit per test 1 second memory limit per test 256 megabytes input standa ...

  5. CodeForces - 233A Perfect Permutation

    A. Perfect Permutation time limit per test: 2 seconds memory limit per test: 256 megabytes input: st ...

  6. Codeforces 863F - Almost Permutation

    863F - Almost Permutation 题意 给出每个位置可以放的数字的范围,定义 \(cost = \sum_{i=1}^{n}(cnt(i))^2\) ,其中 \(cnt(i)\) 为 ...

  7. Codeforces 932.C Permutation Cycle

    C. Permutation Cycle time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. Codeforces 932 C.Permutation Cycle-数学 (ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined))

    C. Permutation Cycle   time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  9. [Codeforces 482A] Diverse Permutation

    [题目链接] https://codeforces.com/contest/482/problem/A [算法] 首先构造一个(k + 1)个数的序列 , 满足它们的差为1-k 对于i > k ...

随机推荐

  1. TraceSource记录程序日志

    1.配置文件 <system.diagnostics> <sources> <source name="TraceError" switchValue ...

  2. js绘制圆形时钟

    纯js制作圆形时钟 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  3. 转载人家写的CURSOR

    转自:http://blog.csdn.net/rdarda/article/details/7881648 1.游标的作用及属性 游标的作用就是用于对查询数据库所返回的记录进行遍历,以便进行相应的操 ...

  4. linux服务器---配置samba

    配置samba使用用户名和密码登录 1.当samba配置文件中的secure设置为user的时候,需要正确的用户名和密码才能登录. root@localhost /]#gedit /etc/samba ...

  5. APP获取证书签名指纹

    Android: public static String getSignatureSHA1(Context context) { String sign = null; try { // 通过包管理 ...

  6. 数据在千万级别上进行全文检索有哪些技术?强大的大数据全文索引解决方案-ClouderaSearch

    数据在千万级别上进行全文检索有哪些技术?强大的大数据全文索引解决方案-ClouderaSearch1.lucene (solr, elasticsearch 都是基于它) 2.sphinx3.elas ...

  7. javaScript的内置对象以及一些常用的方法

    前几天,我们学习了JavaScript的入门课程,但是要想做网站,仅仅学会入门是不够的,今后的几天,我将带领大家精通JavaScript,希望大家好好学习! JS内置对象 String对象:字符串对象 ...

  8. MySQL笔记(六)游标练习

    23.3.1 Trigger Syntax and Examples 意义不明的几道练习,留着备用. 感觉不好写,而且难以调试..不知道以后会不会有实际的应用场景. 环境:MySQL 笔记(三)由 t ...

  9. 如何在 Linux 中将文件编码转换为 UTF-8

    一个文件:vim 文件名.txt输入 :e ++enc=gbk 强制用gbk打开输入 :w ++enc=utf8 转换到utf8保存. 多个文件:for i in *.txt; do iconv -f ...

  10. 《网络对抗》拓展:注入shellcode

    实践三 知识要求: shellcode:指溢出后执行的能开启系统shell的代码.但是在缓冲区溢出攻击时,也可以将整个触发缓冲区溢出攻击过程的代码统称为shellcode,因此可以将shellcode ...