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. LeetCode-EvaluteReversePolishNotation

    题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are + ...

  2. SpringMVC+Spring+Mybatis+Maven+mysql整合

    一.准备工作1.工具:jdk1.7.0_80(64)+tomcat7.0.68+myeclipse10.6+mysql-5.5.48-win322. 开发环境安装配置.Maven项目创建(参考:htt ...

  3. 打造高可靠与高性能的React同构解决方案

    前言 随着React的兴起, 结合Node直出的性能优势和React的组件化,React同构已然成为趋势之一.享受技术福利的同时,直面技术挑战,在复杂场景下,挑战10倍以上极致的性能优化. 什么是同构 ...

  4. 单片机裸机下写一个自己的shell调试器(转)

    源: 单片机裸机下写一个自己的shell调试器

  5. mysql的级联复制和多源复制

    MySQL的复制:https://www.cnblogs.com/wxzhe/p/10051114.html 级联复制的结构如图 我们来设置基于filename和pos的级联复制,并且接受mysql- ...

  6. MySQL数据库 基本操作语句

    操作MySQL数据库 1.创建数据库 create database 数据库名: 2.查看数据库 show databases: 3.选择指定数据库 use 数据库名: 4.删除数据库 drop da ...

  7. 如何在 Linux 中挂载 ISO 文件

    在 Windows 中,我们常常使用 Daemon Tools 和 Virtual CloneDrive 等虚拟光驱软件挂载光盘镜像,下面我们一起来学习在 Linux 中如何挂载 ISO 文件. 在 ...

  8. 20145303刘俊谦《网络攻防》Exp4 Msf基础

    20145303刘俊谦<网络攻防>Exp4 Msf基础 实验目标 • 掌握metasploit的基本应用方式,掌握常用的三种攻击方式的思路. • 一个主动攻击,如ms08_067: • 一 ...

  9. 20145335郝昊《网络攻防》Exp4 MS11_050

    20145335郝昊<网络攻防>Exp4 MS11_050 实验内容 初步掌握平台matesploit的使用 了解漏洞MS11_050漏洞:use-after-free漏洞,即对象被释放之 ...

  10. VC++开机自动启动程序的几种方法 (转载)

    转载:http://blog.csdn.net/zhazhiqiang/article/details/51909703 很多监控软件要求软件能够在系统重新启动后不用用户去点击图标启动项目,而是直接能 ...