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. mysql buffer

    php与mysql的连接有三种方式,mysql,mysqli,pdo.不管使用哪种方式进行连接,都有使用buffer和不使用buffer的区别. 什么叫使用buffer和不使用buffer呢? 客户端 ...

  2. Web前端开发推荐阅读书籍、学习课程下载

    转自http://www.xuanfengge.com/fe-books.html 前言 学校里没有前端的课程,那如何学习JavaScript,又如何使自己成为一个合格的前端工程师呢? 除了在项目中学 ...

  3. [WPF]WPF开发方法论

    纵观Windows GUI应用程序开发方法,从Windows API.MFC到Visual Basic再到.NET Framework,WPF的开发方法论是在.NET Framework方法论的基础上 ...

  4. python webdriver api-右键另存下载文件

    右键另存下载文件 先编辑SciTE脚本: ;ControlFocus("title","text",controlID) ;表示将焦点切换到标题为title窗体 ...

  5. nginx 参考文章汇总

    Nginx 反向代理.负载均衡.页面缓存.URL重写及读写分离详解: http://freeloda.blog.51cto.com/2033581/1288553 Nginx开发从入门到精通: htt ...

  6. DeepMind已将AlphaGo引入多领域 Al泡沫严重

    DeepMind已将AlphaGo引入多领域 Al泡沫严重 在稳操胜券的前提下,谷歌旗下的AlphaGo还是向柯洁下了战书.4月10日,由中国围棋协会.浙江省体育局.谷歌三方联合宣布,将于5月23日至 ...

  7. mysql创建外链失败1005错误解决方法

    mysql创建外链失败1005错误解决方法 错误号:1005错误信息:Can't create table 'webDB.#sql-397_61df' (errno: 150)解决方法 错误原因有四: ...

  8. Q_DECLARE_PRIVATE与Q_DECLARE_PUBLIC

    Q_DECLARE_PRIVATE与Q_DECLARE_PUBLIC 这两个宏在Qt的源码中随处可见,重要性不言而喻.在 部落格的 Inside Qt Series 系列文章中,他用了3篇文章来讲这个 ...

  9. MySQL笔记(四)DDL与DML风格参考

    便于 COPY ▲ 在所有操作之前: SET character_set_database=utf8; 确保 ↓ mysql> SHOW VARIABLES LIKE "%char%& ...

  10. Python学习笔记之在Python中实现单例模式

    有些时候你的项目中难免需要一些全局唯一的对象,这些对象大多是一些工具性的东西,在Python中实现单例模式并不是什么难事.以下总结几种方法: 使用类装饰器 使用装饰器实现单例类的时候,类本身并不知道自 ...