Codeforces Round #590 (Div. 3) E. Special Permutations
链接:
https://codeforces.com/contest/1234/problem/E
题意:
Let's define pi(n) as the following permutation: [i,1,2,…,i−1,i+1,…,n]. This means that the i-th permutation is almost identity (i.e. which maps every element to itself) permutation but the element i is on the first position. Examples:
p1(4)=[1,2,3,4];
p2(4)=[2,1,3,4];
p3(4)=[3,1,2,4];
p4(4)=[4,1,2,3].
You are given an array x1,x2,…,xm (1≤xi≤n).
Let pos(p,val) be the position of the element val in p. So, pos(p1(4),3)=3,pos(p2(4),2)=1,pos(p4(4),4)=1.
Let's define a function f(p)=∑i=1m−1|pos(p,xi)−pos(p,xi+1)|, where |val| is the absolute value of val. This function means the sum of distances between adjacent elements of x in p.
Your task is to calculate f(p1(n)),f(p2(n)),…,f(pn(n)).
思路:
考虑对每个相邻的两个值, 在p上跨过所有点, 在移到前面的时候, 都会减一.
先计算, 每个值移到前面所造成的影响.
再记录每个值相邻的两个, 挨个计算.
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 2e5+10;
int a[MAXN], cnt[MAXN];
LL res[MAXN];
vector<int> vec[MAXN];
int n, m;
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1;i <= m;i++)
scanf("%d", &a[i]);
for (int i = 1;i < m;i++)
res[1] += abs(a[i]-a[i+1]);
for (int i = 1;i < m;i++)
{
int l = a[i], r = a[i + 1];
if (l == r)
continue;
vec[l].push_back(r);
vec[r].push_back(l);
if (l > r)
swap(l, r);
if (r - l < 2)
continue;
cnt[l + 1]++;
cnt[r]--;
}
for (int i = 1;i < n;i++)
cnt[i+1] += cnt[i];
for (int i = 2;i <= n;i++)
{
res[i] = res[1]-cnt[i];
for (auto x: vec[i])
{
res[i] -= abs(i-x);
res[i] += x-1+(x<i);
}
}
for (int i = 1;i <= n;i++)
printf("%I64d ", res[i]);
return 0;
}
Codeforces Round #590 (Div. 3) E. Special Permutations的更多相关文章
- 构造 Codeforces Round #135 (Div. 2) B. Special Offer! Super Price 999 Bourles!
题目传送门 /* 构造:从大到小构造,每一次都把最后不是9的变为9,p - p MOD 10^k - 1,直到小于最小值. 另外,最多len-1次循环 */ #include <cstdio&g ...
- Codeforces Round #590 (Div. 3) Editorial
Codeforces Round #590 (Div. 3) Editorial 题目链接 官方题解 不要因为走得太远,就忘记为什么出发! Problem A 题目大意:商店有n件商品,每件商品有不同 ...
- Codeforces Round #249 (Div. 2) D. Special Grid 枚举
题目链接: http://codeforces.com/contest/435/problem/D D. Special Grid time limit per test:4 secondsmemor ...
- Codeforces Round #590 (Div. 3)
A. Equalize Prices Again 题目链接:https://codeforces.com/contest/1234/problem/A 题意:给你 n 个数 , 你需要改变这些数使得这 ...
- Codeforces Round #590 (Div. 3) D. Distinct Characters Queries(线段树, 位运算)
链接: https://codeforces.com/contest/1234/problem/D 题意: You are given a string s consisting of lowerca ...
- Codeforces Round #590 (Div. 3) C. Pipes
链接: https://codeforces.com/contest/1234/problem/C 题意: You are given a system of pipes. It consists o ...
- Codeforces Round #590 (Div. 3) B2. Social Network (hard version)
链接: https://codeforces.com/contest/1234/problem/B2 题意: The only difference between easy and hard ver ...
- Codeforces Round #590 (Div. 3) A. Equalize Prices Again
链接: https://codeforces.com/contest/1234/problem/A 题意: You are both a shop keeper and a shop assistan ...
- Codeforces Round #590 (Div. 3)(e、f待补
https://codeforces.com/contest/1234/problem/A A. Equalize Prices Again #include<bits/stdc++.h> ...
随机推荐
- axios对请求各种异常情况处理的封装
前端网络请求封装 前端采用了axios来处理网络请求,为了避免在每次请求时都去判断各种各样的网络情况,比如连接超时.服务器内部错误.权限不足等等不一而足,我对axios进行了简单的封装,这里主要使用了 ...
- hdu 1029 求出现次数过半的数
题目传送门//res tp hdu 已知必定存在一个元素出现次数超过一半,考虑用栈 若当前元素等于栈顶元素,入栈,反之出栈,并将当前元素入栈 最终的栈顶元素即是所求 #include<iostr ...
- composer在windows下安装并且设置全局变量
Composer是 PHP 用来管理依赖(dependency)关系的工具.你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer 会帮你安装这些依赖的库文件. 1丶使用安 ...
- centos7 yum安装nginx和 编译安装tengine
说明 我这里给大家演示一下如何安装nginx,nginx我就不多介绍了,然后我再说一点就是,安装的两种方法都可以,编译安装和yum安装,我不能每个都演示两遍呀,所以看到我这博客的你,学会举一反三好吧? ...
- 第十三章 ZYNQ-MIZ702 PL中断请求
本篇文章主要介绍外设(PL)产生的中断请求,在PS端进行处理. 在PL端通过按键产生中断,PS接受到之后点亮相应的LED. 本文所使用的开发板是Miz702 PC 开发环境版本:Vivado 2015 ...
- VMware与主机联网设置
1.编辑-虚拟机网络编辑器 2.虚拟机设置 3. 4.主机ping虚拟机
- windows服务与log4net应用
有时候我们需要用到window服务来执行定时任务,然后配合log4net记录程序运行情况,这里简单记录下配置的整个过程以及注意要点: 一.添加windows服务 1.设计页面,右键添加安装程序
- SpringBoot整合Mybatis问题
IDEA对xml文件处理的方式不同 在Eclipse中到dao文件与mapper.xml文件只要在同一级目录下即可 在IDEA中mapper.xml要放在resources目录下 注:resource ...
- linux 目录结构及VIM
目录结构及VIM 文件系统 说明: 文件系统就是操作管理存储设备或分区上的文件的方法和数据结构,也就是存储设备上组织文件的方式. 操作系统中负责管理和存储文件信息的软件机构叫文件管理系统,简称为文件系 ...
- Windows下解决系统端口被VM虚拟机占用问题
一)问题背景 安装VM虚拟机后,经常会遇到启动其他程序时出现端口被占用的情况,其中以80端口被占用最为常见. 二)解决思路 解除或更改被占用端口号,但是更改端口号时,除非对系统的端口占用情况非常熟悉, ...