【题解】CF359B Permutation

【题解】CF359B Permutation

求一个长度为(2n)的序列,满足(Sigma |a_{2i}-a_{2i-1}|-|Sigma a_{2i}-a_{2i-1}|=2k)

这种带绝对值的题目套路就是把绝对值拆开。看看(n=2)时候的情况

(left[1,2,3,4 ight])

(|2-1|+|4-3|-|2-1+4-3|=0)

(swap(1,2) =>)

(|1-2|+|4-3|-|1-2+4-3|=2)

也就是交换一组产生(2)的贡献,直接交换(k)组就好了。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<bitset>
#include<vector>
#include<map>
#include<ctime>
#include<cstdlib>
#include<set>
#include<bitset>
#include<stack>
#include<list>
#include<cmath>
using namespace std;
#define RP(t,a,b) for(register int (t)=(a),edd_=(b);t<=edd_;++t)
#define DRP(t,a,b) for(register int (t)=(a),edd_=(b);t>=edd_;--t)
#define ERP(t,a) for(int t=head[a];t;t=e[t].nx)
#define Max(a,b) ((a)<(b)?(b):(a))
#define Min(a,b) ((a)<(b)?(a):(b))
#define TMP template<class ccf>
#define lef L,R,l,mid,pos<<1
#define rgt L,R,mid+1,r,pos<<1|1
#define midd register int mid=(l+r)>>1
#define chek if(R<l||r<L)return
#define all 1,n,1
#define pushup(x) seg[(x)]=seg[(x)<<1]+seg[(x)<<1|1]
typedef long long ll;
TMP inline ccf qr(ccf k){
    char c=getchar();
    ccf x=0;
    int q=1;
    while(c<48||c>57)
    q=c==45?-1:q,c=getchar();
    while(c>=48&&c<=57)
    x=x*10+c-48,c=getchar();
    if(q==-1)
    x=-x;
    return x;
}
const int maxn=5e4+15;
int data[maxn<<1];
int n,k;

int main(){
#ifndef ONLINE_JUDGE
    freopen("in.in","r",stdin);
    freopen("out.out","w",stdout);
#endif
    n=qr(1);
    k=qr(1);
    RP(t,1,n*2+1)
    data[t]=t;
    RP(t,1,k)
    swap(data[t<<1],data[(t<<1)-1]);
    RP(t,1,n*2-1)
    cout<<data[t]<<' ';
    cout<<data[n*2];
    return 0;
}



原文地址:https://www.cnblogs.com/winlere/p/10332915.html