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> ...
随机推荐
- [Python3 填坑] 004 关于八进制
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 问题的由来 2.2 问题的解决 2.2.1 先说结论 2.2.2 八进制的用途 2.2.3 少废话,上例子 1. print( 坑的信息 ...
- Scala当中parallelize并行化的用法
[学习笔记] parallelize并行化集合是根据一个已经存在的Scala集合创建的RDD对象.集合的里面的元素将会被拷贝进入新创建出的一个可被并行操作的分布式数据集.例如:val rdd03 = ...
- java日期比较工具类
package com.net.util; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.ut ...
- java 自带 http get/post 请求
请求参数,请求参数应该是 name1=value1&name2=value2 的形式. import java.io.BufferedReader; import java.io.IOExce ...
- 关于typescript中的枚举你需要知道这些
数字枚举 数字枚举,即枚举里所有属性的值都是数字类型,先看这段代码: enum Colors { Red, Blue, Yellow } console.log(Colors.Red) console ...
- typora的基本使用技巧汇总
typora的基本使用技巧汇总 链接: https://www.jianshu.com/p/380005c8f104
- 装饰器中functools的用处
定义一个最简单的装饰器 def user_login_data(f): def wrapper(*args, **kwargs): return f(*args, **kwargs) return w ...
- 适合新手的160个creakme(二)
先跑一下,然后找出关键字符串 关键字符串是You Get Wrong和Try Again,不过IDA好像识别不出来这个字符串,在Ollydbg中右键Search For,寻找所有字符串,可以找到这些字 ...
- 一个jenkins的bug
部署在redhat上docker中的jenkins运行了很久了,最新发现一个站点不是最新的,于是去查看发现git仓库都连不上,我换成外网github的仓库就可以连非得是https吗???? 提示是域名 ...
- Pattern Recognition and Machine Learning-01-Preface
Preface Pattern recognition has its origins in engineering, whereas machine learning grew out of com ...