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> ...
随机推荐
- 【转帖】sysbench的安装和做性能测试
iMySQL | 老叶茶馆 sysbench的安装和做性能测试 http://imysql.cn/node/312 我仿照这个学的 但是 需要用更新的版本才可以. By yejr on 14 六月 ...
- Centos安装elasticsearch,php连接使用
一.下载安装JAVA 下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ...
- xshell和xftp过期解决办法
去官网 xshell:https://www.netsarang.com/download/down_form.html?code=522 xftp:https://www.netsarang.com ...
- .Net C# Dictionary 和参数字符串互转
#region Parse #region Dictionary Parse To String /// <summary> /// Dictionary Parse To String ...
- DDD 理解
DDD提倡充血模型,业务放在类中,而不是服务中,刚开始是比较不清楚的.突然明白,以前开发桌面程序的时候,不就是这样处理了吗?业务分析和代码实现一一对应.因为桌面程序没有数据库,他就是纯粹的面向对象的实 ...
- 使用lodop.js打印控件打印table并分页等
import {getLodop} from '@/utils/LodopFuncs.js' //打印表格 export default{ // num 打印还是打印预览 conData 对象形式 传 ...
- 重拾MVC——第二天:Vue学习与即时密码格式验证
今天是复习MVC的第二天,准备自己写一个后台管理,然后慢慢写大,做全. 个人感觉做 Web 的,前端知识是必备的,所有今天学习了一下 Vue,很多人用这个,我以前没有用过,今天把它补起来. 比较了各个 ...
- 浅谈Promise原理与应用
在JavaScript中,所有代码都是单线程.由于该“缺陷”,JavaScript在处理网络操作.事件操作时都是需要进行异步执行的.AJAX就是一个典型的异步操作 对于异步操作,有传统的利用回调函数和 ...
- 我理解的epoll(二)——ET、LT的实例分析
https://www.cnblogs.com/yuuyuu/p/5103744.html 这篇文章已经写的很清楚了,暂时不展开论述了. http://blog.csdn.net/weiyuefei/ ...
- RabbitMQ 功能
学习完了rabbitmq总一下 RabbitMQ依赖的语言 erlang 第一它可以实现不同程序之间的程序信息储存交互,在易用性.扩展性.高可用性的方面不俗. rabbitmq相当于一个中间人,我们同 ...