[Codeforces1234E]Special Permutations

题意

给定一个长度为mmm的序列xxx

记:

排列pi(n)=[i,1,2,...,i−1,i+1,...,n]p_i(n)=[i,1,2,...,i-1,i+1,...,n]pi(n)=[i,1,2,...,i1,i+1,...,n]
pos(p,x)pos(p,x)pos(p,x)表示xxx在排列ppp中的第几位
f(p)=∑i=1m−1∣pos(p,xi)−pos(p,xi+1)∣f(p)=\sum_{i=1}^{m-1}|pos(p,x_i)-pos(p,x_{i+1})|f(p)=i=1m1pos(p,xi)pos(p,xi+1)

f(p1(n)),f(p2(n)),...,f(pn(n))f(p_1(n)),f(p_2(n)),...,f(p_n(n))f(p1(n)),f(p2(n)),...,f(pn(n))

2≤n,m≤2×105,1≤xi≤n2\le n,m\le2\times10^5,1\le x_i\le n2n,m2×105,1xin


题解

方法一:暴力

观察pi−1(n)p_{i-1}(n)pi1(n)pi(n)p_i(n)pi(n)

pi−1(n)=[i−1,1,2,...,i,i+1,...,n]p_{i-1}(n)=[i-1,1,2,...,i,i+1,...,n]pi1(n)=[i1,1,2,...,i,i+1,...,n]

pi(n)=[i,1,2,...,i−1,i+1,...,n]p_i(n)=[i,1,2,...,i-1,i+1,...,n]pi(n)=[i,1,2,...,i1,i+1,...,n]

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

pos(pi(n),i)=1,pos(pi(n),i−1)=ipos(p_i(n),i)=1,pos(p_i(n),i-1)=ipos(pi(n),i)=1,pos(pi(n),i1)=i

其余的pos(pi(n),j)=pos(pi−1(n),j)∣j≠i,j≠i−1pos(p_i(n),j)=pos(p_{i-1}(n),j)|j\neq i,j\neq i-1pos(pi(n),j)=pos(pi1(n),j)j=i,j=i1

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

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

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

时间复杂度O(n+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)l,r(l<r),考虑他们在所有的pi(n)p_i(n)pi(n)中的相对位置(((即对答案的贡献)))

1≤i<l1\le i<l1i<l时:l,rl,rl,r相对位置不变,贡献均为r−lr-lrl
i=li=li=l时:lll被移到第一位,rrr不变,贡献为r−1r-1r1
l<i<rl<i<rl<i<r时:每次都是lllrrr中的一个数被移到第一位,l,rl,rl,r距离−1-11,贡献均为r−l−1r-l-1rl1
i=ri=ri=r时:rrr被移到第一位,lll不变,贡献为lll
r<i≤nr<i\le nr<in时:l,rl,rl,r相对位置不变,贡献均为r−lr-lrl

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

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

时间复杂度O(n+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];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;
}
### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值