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. PHP获取http头信息

    PHP手册提供了现成的函数: getallheaders (PHP 4, PHP 5) getallheaders — Fetch all HTTP request headers 说明 array ...

  2. linux服务后台管理

    把进程放到后台有两种方法 1.cmmand & 2.ctrl+z    暂停到后台 查看后台服务  jobs 把后台进程移到前台 fg  %2 工作号  恢复到前台 后台服务继续执行 bg  ...

  3. #C++初学记录(深度搜索#递归)

    深度搜索 走地图的题目是深度搜索里比较容易理解的题目,更深层次的是全排列和七皇后等经典题目,更加难以理解,代码比较抽象. 题目:红与黑 蒜厂有一间长方形的房子,地上铺了红色.黑色两种颜色的正方形瓷砖. ...

  4. cmd重启服务器,有时不想去机房,并且远程桌面连接登录不上了

    有时不想去机房,并且远程桌面连接登录不上了,需要远程重启服务器的,这时可以使用命令行方式远程重启.在cmd命令行状态下输入:shutdown -r -m \\192.168.1.10 -t 0 -f ...

  5. zw版【转发·台湾nvp系列Delphi例程】HALCON HomMat2dRotate1

    zw版[转发·台湾nvp系列Delphi例程]HALCON HomMat2dRotate1 procedure TForm1.Button1Click(Sender: TObject);var img ...

  6. OS X 下动态库的引用

    foo.c #include <stdio.h> void foo(void) { printf("foo.\n"); } main.c #include <st ...

  7. 线程等待——CountDownLatch使用

    告警性能优化过程中,遇到如下问题:1. 在数据库计算几十万个实体的KPI值的方差:2. 计算结果进行表格化处理. 这里KPI包含多个Counter的数据库函数运算(比如Decode,AVG等函数),方 ...

  8. 学写网页 #04# w3school

    索引: HTML 输入类型 XHTML HTML5 HTML5 样式指南和代码约定 WHO 成立于 1948 年. 对缩写进行标记能够为浏览器.翻译系统以及搜索引擎提供有用的信息. code 元素不保 ...

  9. Navicat连不上mysql8

    今天使用navicat连接mysql8,发现错误连连 错误1:1130-Host '192.168.50.2' is not allowed to connect to this MySQL serv ...

  10. [Linux 003]——用户和用户组以及 Linux 权限管理(一)

    嗬!没想到吧!学习 Linux 的第三天,我们已经开始接触用户管理,用户组管理,以及权限管理这几个逼格满满的关键字.这几个关键字对于前端程序猿的我来说真的是很高大上有木有,以前尝试学 Linux 的时 ...