链接:

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的更多相关文章

  1. 构造 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 ...

  2. Codeforces Round #590 (Div. 3) Editorial

    Codeforces Round #590 (Div. 3) Editorial 题目链接 官方题解 不要因为走得太远,就忘记为什么出发! Problem A 题目大意:商店有n件商品,每件商品有不同 ...

  3. Codeforces Round #249 (Div. 2) D. Special Grid 枚举

    题目链接: http://codeforces.com/contest/435/problem/D D. Special Grid time limit per test:4 secondsmemor ...

  4. Codeforces Round #590 (Div. 3)

    A. Equalize Prices Again 题目链接:https://codeforces.com/contest/1234/problem/A 题意:给你 n 个数 , 你需要改变这些数使得这 ...

  5. 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 ...

  6. Codeforces Round #590 (Div. 3) C. Pipes

    链接: https://codeforces.com/contest/1234/problem/C 题意: You are given a system of pipes. It consists o ...

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

  8. 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 ...

  9. Codeforces Round #590 (Div. 3)(e、f待补

    https://codeforces.com/contest/1234/problem/A A. Equalize Prices Again #include<bits/stdc++.h> ...

随机推荐

  1. MySQL之基础认识与操作

    MySQL数据库 开发学习中,想满足一些需求,无疑需要经常与数据打交道,例如,我们在使用IO的一些技术的时候,常常需要将一些数据存储到外部文件,可能大家会问,我们初学的时候常常会简单的保存一些数据到 ...

  2. HanLP-基于HMM-Viterbi的人名识别原理介绍

    Hanlp自然语言处理包中的基于HMM-Viterbi处理人名识别的内容大概在年初的有分享过这类的文章,时间稍微久了一点,有点忘记了.看了 baiziyu 分享的这篇比我之前分享的要简单明了的多.下面 ...

  3. SQL中的关键词

    AS的用法及妙用 https://www.cnblogs.com/zhaotiancheng/p/6692553.html

  4. oracle 创建新用户,授权dba

    1.用有dba权限的用户登录:sys用户 2.创建一个新用户:create user abc identified by 123456; 3.授予DBA权限: grant connect,resour ...

  5. LC 202. Happy Number

    问题描述 Write an algorithm to determine if a number is "happy". A happy number is a number de ...

  6. 级联-city

    <!DOCTYPE html><html> <head>  <meta charset="UTF-8">  <title> ...

  7. LRU算法简介

    LRU是什么? 按照英文的直接原义就是Least Recently Used,最近最久未使用法,它是按照一个非常注明的计算机操作系统基础理论得来的:最近使用的页面数据会在未来一段时期内仍然被使用,已经 ...

  8. 操作系统diy-1-资料整理

    已经研三了,前段时间校招找了份内核开发的工作,正好有时间做这个以前一直想做的事情.听说写操作系统要花很多时间了解学习计算机方方面面的知识,之前也查过相关的资料,关注过mit的操作系统公开课程.这几天准 ...

  9. 10.使用du将文件按大小进行排序

    按G进行排序du -sh * | grep G | sort -nr

  10. js 怎样判断用户是否在浏览当前页面

    有些时候我们需要在项目中判断用户是否在浏览当前页面,或者当前页面是否处于激活状态.然后再进行相关的操作.浏览器中可通过window对象的onblur.onfocus判断,或者document的hidd ...