[Codeforces1234E]Special Permutations

题意

给定一个长度为 m m 的序列 x x

记:

排列 p i ( n ) = [ i , 1 , 2 , . . . , i 1 , i + 1 , . . . , n ] p_i(n)=[i,1,2,...,i-1,i+1,...,n]
p o s ( p , x ) pos(p,x) 表示 x x 在排列 p p 中的第几位
f ( p ) = i = 1 m 1 p o s ( p , x i ) p o s ( p , x i + 1 ) f(p)=\sum_{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 n , m 2 × 1 0 5 , 1 x i n 2\le n,m\le2\times10^5,1\le x_i\le n


题解

方法一:暴力

观察 p i 1 ( n ) p_{i-1}(n) p i ( n ) p_i(n)

p i 1 ( n ) = [ i 1 , 1 , 2 , . . . , i , i + 1 , . . . , n ] p_{i-1}(n)=[i-1,1,2,...,i,i+1,...,n]

p i ( n ) = [ i , 1 , 2 , . . . , i 1 , i + 1 , . . . , n ] p_i(n)=[i,1,2,...,i-1,i+1,...,n]

可以发现只是交换了一下 i 1 i-1 i i 的位置

p o s ( p i ( n ) , i ) = 1 , p o s ( p i ( n ) , i 1 ) = i pos(p_i(n),i)=1,pos(p_i(n),i-1)=i

其余的 p o s ( p i ( n ) , j ) = p o s ( p i 1 ( n ) , j ) j i , j i 1 pos(p_i(n),j)=pos(p_{i-1}(n),j)|j\neq i,j\neq i-1

所以每次交换只会改变两种数 ( i 1 (i-1 i ) i) 与左右的数的差的绝对值

记下每一种数所在的位置,每次枚举这两种数的所有位置,暴力修改答案

由于每种数最多只会被枚举两次,且总共只有 m m 个数,所以总的时间还是线性的

时间复杂度 O ( n + m ) O(n+m)

#include<bits/stdc++.h>
#define fp(i,a,b) for(register int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(register int i=a,I=b-1;i>I;--i)
#define go(u) for(register int i=fi[u],v=e[i].to;i;v=e[i=e[i].nx].to)
#define file(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
char ss[1<<17],*A=ss,*B=ss;
inline char gc(){return A==B&&(B=(A=ss)+fread(ss,1,1<<17,stdin),A==B)?-1:*A++;}
template<class T>inline void sd(T&x){
    char c;T y=1;while(c=gc(),(c<48||57<c)&&c!=-1)if(c==45)y=-1;x=c-48;
    while(c=gc(),47<c&&c<58)x=x*10+c-48;x*=y;
}
#define ps sr[++C]='\n'
char sr[1<<21],z[20];int C=-1,Z;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
template<class T>inline void we(T x){
    if(C>1<<20)Ot();if(x<0)sr[++C]=45,x=-x;
    while(z[++Z]=x%10+48,x/=10);
    while(sr[++C]=z[Z],--Z);sr[++C]=' ';
}
const int N=2e5+5;
typedef long long ll;
typedef int arr[N];
int n,m,a[N],p[N];ll ans;
vector<int>pos[N];
int main(){
	#ifndef ONLINE_JUDGE
		file("s");
	#endif
	sd(n),sd(m);
	fp(i,1,n)p[i]=i;
	fp(i,1,m)sd(a[i]),pos[a[i]].push_back(i);
	fp(i,1,m-1)ans+=abs(a[i]-a[i+1]);
	we(ans);
	fp(i,2,n){	
		fp(j,0,pos[i].size()-1){
			int k=pos[i][j];
			if(k>1)ans-=abs(i-p[a[k-1]]);
			if(k<m)ans-=abs(i-p[a[k+1]]);
		}
		fp(j,0,pos[i-1].size()-1){
			int k=pos[i-1][j];
			if(k>1&&a[k-1]!=i)ans-=abs(1-p[a[k-1]]);
			if(k<m&&a[k+1]!=i)ans-=abs(1-p[a[k+1]]);
		}
		p[i]=1;p[i-1]=i;
		fp(j,0,pos[i].size()-1){
			int k=pos[i][j];
			if(k>1)ans+=abs(1-p[a[k-1]]);
			if(k<m)ans+=abs(1-p[a[k+1]]);
		}
		fp(j,0,pos[i-1].size()-1){
			int k=pos[i-1][j];
			if(k>1&&a[k-1]!=i)ans+=abs(i-p[a[k-1]]);
			if(k<m&&a[k+1]!=i)ans+=abs(i-p[a[k+1]]);
		}
		we(ans);
	}
return Ot(),0;
}
//Tips:注意序列长度是m不是n!!!!!

方法二:差分+前缀和

对于某一对相邻的数 l , r ( l < r ) l,r(l<r) ,考虑他们在所有的 p i ( n ) p_i(n) 中的相对位置 ( ( 即对答案的贡献 ) )

1 i < l 1\le i<l 时: l , r l,r 相对位置不变,贡献均为 r l r-l
i = l i=l 时: l l 被移到第一位, r r 不变,贡献为 r 1 r-1
l < i < r l<i<r 时:每次都是 l l r r 中的一个数被移到第一位, l , r l,r 距离 1 -1 ,贡献均为 r l 1 r-l-1
i = r i=r 时: r r 被移到第一位, l l 不变,贡献为 l l
r < i n r<i\le n 时: l , r l,r 相对位置不变,贡献均为 r l r-l

于是这题就变成区间修改和按顺序单点查询了

用差分数组快速修改 f ( p i ( n ) ) f(p_i(n)) ,最后求一遍前缀和即可

时间复杂度 O ( n + m ) O(n+m)

#include<bits/stdc++.h>
#define fp(i,a,b) for(register int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(register int i=a,I=b-1;i>I;--i)
#define go(u) for(register int i=fi[u],v=e[i].to;i;v=e[i=e[i].nx].to)
#define file(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
char ss[1<<17],*A=ss,*B=ss;
inline char gc(){return A==B&&(B=(A=ss)+fread(ss,1,1<<17,stdin),A==B)?-1:*A++;}
template<class T>inline void sd(T&x){
    char c;T y=1;while(c=gc(),(c<48||57<c)&&c!=-1)if(c==45)y=-1;x=c-48;
    while(c=gc(),47<c&&c<58)x=x*10+c-48;x*=y;
}
#define ps sr[++C]='\n'
char sr[1<<21],z[20];int C=-1,Z;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
template<class T>inline void we(T x){
    if(C>1<<20)Ot();if(x<0)sr[++C]=45,x=-x;
    while(z[++Z]=x%10+48,x/=10);
    while(sr[++C]=z[Z],--Z);sr[++C]=' ';
}
const int N=2e5+5;
typedef long long ll;
typedef int arr[N];
int n,m,a[N];ll f[N];
inline void mdy(int l,int r,int x){f[l]+=x,f[r+1]-=x;}
int main(){
	#ifndef ONLINE_JUDGE
		file("s");
	#endif
	sd(n),sd(m);
	fp(i,1,m)sd(a[i]);
	fp(i,1,m-1){
		int l=a[i],r=a[i+1];
		if(l==r)continue;
		if(l>r)swap(l,r);
		mdy(1,l-1,r-l);
		mdy(l,l,r-1);
		mdy(l+1,r-1,r-l-1);
		mdy(r,r,l);
		mdy(r+1,n,r-l);
	}
	fp(i,1,n)we(f[i]+=f[i-1]);
return Ot(),0;
}

猜你喜欢

转载自blog.csdn.net/BeNoble_/article/details/101985236
今日推荐