题目描述
Let’s define p_i(n)p
i
(n) as the following permutation: [i, 1, 2, \dots, i - 1, i + 1, \dots, n][i,1,2,…,i−1,i+1,…,n] . This means that the ii -th permutation is almost identity (i.e. which maps every element to itself) permutation but the element ii is on the first position. Examples:
p_1(4) = [1, 2, 3, 4]p
1
(4)=[1,2,3,4] ;
p_2(4) = [2, 1, 3, 4]p
2
(4)=[2,1,3,4] ;
p_3(4) = [3, 1, 2, 4]p
3
(4)=[3,1,2,4] ;
p_4(4) = [4, 1, 2, 3]p
4
(4)=[4,1,2,3] .
You are given an array x_1, x_2, \dots, x_mx
1
,x
2
,…,x
m
( 1 \le x_i \le n1≤x
i
≤n ).
Let pos(p, val)pos(p,val) be the position of the element valval in pp . So, pos(p_1(4), 3) = 3, pos(p_2(4), 2) = 1, pos(p_4(4), 4) = 1pos(p
1
(4),3)=3,pos(p
2
(4),2)=1,pos(p
4
(4),4)=1 .
Let’s define a function f§ = \sum\limits_{i=1}^{m - 1} |pos(p, x_i) - pos(p, x_{i + 1})|f§=
i=1
∑
m−1
∣pos(p,x
i
)−pos(p,x
i+1
)∣ , where |val|∣val∣ is the absolute value of valval . This function means the sum of distances between adjacent elements of xx in pp .
Your task is to calculate f(p_1(n)), f(p_2(n)), \dots, f(p_n(n))f(p
1
(n)),f(p
2
(n)),…,f(p
n
(n)) .
输入格式
The first line of the input contains two integers nn and mm ( 2 \le n, m \le 2 \cdot 10^52≤n,m≤2⋅10
5
) — the number of elements in each permutation and the number of elements in xx .
The second line of the input contains mm integers ( mm , not nn ) x_1, x_2, \dots, x_mx
1
,x
2
,…,x
m
( 1 \le x_i \le n1≤x
i
≤n ), where x_ix
i
is the ii -th element of xx . Elements of xx can repeat and appear in arbitrary order.
输出格式
Print nn integers: f(p_1(n)), f(p_2(n)), \dots, f(p_n(n))f(p
1
(n)),f(p
2
(n)),…,f(p
n
(n)) .
题意翻译
给定一个长度为mm的序列xx
记:
排列p_i(n)=[i,1,2,…,i-1,i+1,…,n]p
i
(n)=[i,1,2,…,i−1,i+1,…,n] pos(p,x)pos(p,x)表示xx在排列pp中的第几位 f§=\sum_{i=1}^{m-1}|pos(p,x_i)-pos(p,x_{i+1})|f§=∑
i=1
m−1
∣pos(p,x
i
)−pos(p,x
i+1
)∣
求f(p_1(n)),f(p_2(n)),…,f(p_n(n))f(p
1
(n)),f(p
2
(n)),…,f(p
n
(n))。
2\le n,m\le2\times10^5,1\le x_i\le n2≤n,m≤2×10
5
,1≤x
i
≤n
输入输出样例
输入 #1复制
4 4
1 2 3 4
输出 #1复制
3 4 6 5
输入 #2复制
5 5
2 1 5 3 5
输出 #2复制
9 8 12 6 8
输入 #3复制
2 10
1 2 1 1 2 2 2 2 2 2
输出 #3复制
3 3
说明/提示
Consider the first example:
x = [1, 2, 3, 4]x=[1,2,3,4] , so
for the permutation p_1(4) = [1, 2, 3, 4]p
1
(4)=[1,2,3,4] the answer is |1 - 2| + |2 - 3| + |3 - 4| = 3∣1−2∣+∣2−3∣+∣3−4∣=3 ;
for the permutation p_2(4) = [2, 1, 3, 4]p
2
(4)=[2,1,3,4] the answer is |2 - 1| + |1 - 3| + |3 - 4| = 4∣2−1∣+∣1−3∣+∣3−4∣=4 ;
for the permutation p_3(4) = [3, 1, 2, 4]p
3
(4)=[3,1,2,4] the answer is |2 - 3| + |3 - 1| + |1 - 4| = 6∣2−3∣+∣3−1∣+∣1−4∣=6 ;
for the permutation p_4(4) = [4, 1, 2, 3]p
4
(4)=[4,1,2,3] the answer is |2 - 3| + |3 - 4| + |4 - 1| = 5∣2−3∣+∣3−4∣+∣4−1∣=5 .
Consider the second example:
x = [2, 1, 5, 3, 5]x=[2,1,5,3,5] , so
for the permutation p_1(5) = [1, 2, 3, 4, 5]p
1
(5)=[1,2,3,4,5] the answer is |2 - 1| + |1 - 5| + |5 - 3| + |3 - 5| = 9∣2−1∣+∣1−5∣+∣5−3∣+∣3−5∣=9 ;
for the permutation p_2(5) = [2, 1, 3, 4, 5]p
2
(5)=[2,1,3,4,5] the answer is |1 - 2| + |2 - 5| + |5 - 3| + |3 - 5| = 8∣1−2∣+∣2−5∣+∣5−3∣+∣3−5∣=8 ;
for the permutation p_3(5) = [3, 1, 2, 4, 5]p
3
(5)=[3,1,2,4,5] the answer is |3 - 2| + |2 - 5| + |5 - 1| + |1 - 5| = 12∣3−2∣+∣2−5∣+∣5−1∣+∣1−5∣=12 ;
for the permutation p_4(5) = [4, 1, 2, 3, 5]p
4
(5)=[4,1,2,3,5] the answer is |3 - 2| + |2 - 5| + |5 - 4| + |4 - 5| = 6∣3−2∣+∣2−5∣+∣5−4∣+∣4−5∣=6 ;
for the permutation p_5(5) = [5, 1, 2, 3, 4]p
5
(5)=[5,1,2,3,4] the answer is |3 - 2| + |2 - 1| + |1 - 4| + |4 - 1| = 8∣3−2∣+∣2−1∣+∣1−4∣+∣4−1∣=8 .
可以发现通过翻转操作我们可以将任意两个子串拼在一起。
这样子串所在的位置就不重要了,只需要记录字符的状态即可。
注意到 ∣ | ∣ 字符集 ∣ ≤ 20 |\le20 ∣≤20 ,考虑状压记录所有的字符集状态。
设 f s f_s fs 表示状态 s s s 中有多少字母(其实就是 s s s 的二进制中有多少 1 1 1 ),设全集为 T T T 。
那么:
f s = max 1 ≤ i ≤ j ≤ ∣ S ∣ ( j − i + 1 ∣ S i ≠ S i + 1 ≠ . . . ≠ S j , s = S i + . . . + S j ) f_s=\max_{1\le i\le j\le|S|}(j-i+1|S_i\neq S_{i+1}\neq...\neq S_j,s=S_i+...+S_j) fs=1≤i≤j≤∣S∣max(j−i+1∣Si=Si+1=...=Sj,s=Si+...+Sj)
所以:
A n s = max s ⊆ T ( f s + max t ⊆ T − s f t ) Ans=\max_{s\subseteq T}(f_s+\max_{t\subseteq T-s}f_t) Ans=s⊆Tmax(fs+t⊆T−smaxft)
可以发现后面的枚举子集完全可以预处理出来。
记 g s = max t ⊆ s f t g_s=\max_{t\subseteq s}f_t gs=maxt⊆sft ,则 A n s = max s ⊆ T ( f s + g T − s ) Ans=\max_{s\subseteq T}(f_s+g_{T-s}) Ans=maxs⊆T(fs+gT−s)
总时间复杂度 O ( ∣ S ∣ ∣ T ∣ + ∣ T ∣ 2 ∣ T ∣ ) O(|S||T|+|T|2^{|T|}) O(∣S∣∣T∣+∣T∣2∣T∣)
#include
using namespace std;
const int N = 20;
const int M = 10e6 + 10;
int dp[1 << N];
int main() {
char s[M];
scanf("%s", s);
int len = strlen(s);
for (int i = 0; i < len; i ++ ) {
int now = 0;
for (int j = 0; j < N && i + j < len; j ++ ) {
int ch = s[i + j] - 'a';
if (now & (1 << ch)) {
break ;
}
now |= (1 << ch);
dp[now] = j + 1;
}
}
for (int now = 0; now < (1 << N); now ++ ) {
for (int i = 0; i < N; i ++ ) {
if (now & (1 << i)) {
dp[now] = max(dp[now], dp[now ^ (1 << i)]);
}
}
}
int ans = 0;
for (int now = 0; now < (1 << N); now ++ ) {
int tmp = ((1 << N) - 1) ^ now;
ans = max(ans, dp[now] + dp[tmp]);
}
printf("%d\n", ans);
return 0;
}