【题目链接】 http://codeforces.com/contest/804/problem/E

【题目大意】

  给出一个1到n的排列,问每两个位置都进行一次交换最终排列不变是否可能,
  如果可能输出交换顺序。

【题解】

  我们发现对于四个一组组内进行六次交换之后可以保证四个数的位置不变,
  而对于每组相互之间可以一共进行十六次交换使得两组均不会发生变化
  所以如果n能被4整除,那么我们可以4个分组达到目的,
  当不能被4整除的时候,我们发现余数为2和3的时候都不能构造出可行解。
  在余数为1的时候有一种特殊的基于4分组的构造法,
  我们在相邻两个数字交换的时候,我们将多出来的那个数字作为第三参数,
  这样多出来那个数字就能够和剩余所有位置交换过一次而不改变位置。

【代码】

#include <cstdio>
#include <algorithm>
#include <utility>
#include <vector>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
typedef pair<int,int> P;
typedef vector<P> V;
void add(V &ans,int pos1,int pos2){
pos1<<=2; pos2<<=2;
rep(k,4)rep(i,4)ans.push_back(P(pos1+i,pos2+(i^k)));
}
int dx[]={0,0,1,0,1,2},dy[]={1,2,3,3,2,3};
void add4(V &ans,int pos){
pos<<=2;
rep(i,6)ans.push_back(P(pos+dx[i],pos+dy[i]));
}
V make4(int n){
V ans;
rep(i,n/4)add4(ans,i);
rep(i,n/4)rep(j,i)add(ans,j,i);
return ans;
}
int n;
void solve(){
V ans;
if(n%4==0)ans=make4(n);
else if(n%4==1){
V tmp=make4(n-1);
for(int i=0;i<tmp.size();i++){
P p=tmp[i];
int x=p.first,y=p.second;
if(x>y)swap(x,y);
if(y==x+1&&y%2){
ans.push_back(P(n-1,x));
ans.push_back(P(x,y));
ans.push_back(P(n-1,y));
}else ans.push_back(p);
}
}else{puts("NO");return;}
puts("YES");
for(int i=0;i<ans.size();i++){
P p=ans[i];
if(p.first>p.second)swap(p.first,p.second);
printf("%d %d\n",p.first+1,p.second+1);
}
}
int main(){
while(~scanf("%d",&n))solve();
return 0;
}

Codeforces 804E The same permutation(构造)的更多相关文章

  1. codeforces 622C. Optimal Number Permutation 构造

    题目链接 假设始终可以找到一种状态使得值为0, 那么两个1之间需要隔n-2个数, 两个2之间需要隔n-3个数, 两个3之间隔n-4个数. 我们发现两个三可以放到两个1之间, 同理两个5放到两个3之间. ...

  2. Educational Codeforces Round 7 D. Optimal Number Permutation 构造题

    D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...

  3. Codeforces Round #275 (Div. 1)A. Diverse Permutation 构造

    Codeforces Round #275 (Div. 1)A. Diverse Permutation Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 ht ...

  4. Codeforces Round #309 (Div. 1) B. Kyoya and Permutation 构造

    B. Kyoya and Permutation Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...

  5. Codeforces 482 - Diverse Permutation 构造题

    这是一道蛮基础的构造题. - k         +(k - 1)      -(k - 2) 1 + k ,    1 ,         k ,             2,    ....... ...

  6. codeforces C. Diverse Permutation(构造)

    题意:1...n 的全排列中 p1, p2, p3....pn中,找到至少有k个 |p1-p2| , |p2-p3|, ...|pn-1 - pn| 互不相同的元素! 思路: 保证相邻的两个数的差值的 ...

  7. Codeforces.612E.Square Root of Permutation(构造)

    题目链接 \(Description\) 给定一个\(n\)的排列\(p_i\),求一个排列\(q_i\),使得对于任意\(1\leq i\leq n\),\(q_{q_i}=p_i\).无解输出\( ...

  8. codeforces B. Levko and Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/361/B 题目意思:有n个数,这些数的范围是[1,n],并且每个数都是不相同的.你需要构造一个排列,使得这 ...

  9. Codeforces 691D Swaps in Permutation

    Time Limit:5000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Prac ...

随机推荐

  1. 【Zigbee技术入门教程-02】一图读懂ZStack协议栈的核心思想与工作机理

    [Zigbee技术入门教程-02]一图读懂ZStack协议栈的核心思想与工作机理 广东职业技术学院  欧浩源   Z-Stack协议栈是一个基于任务轮询方式的操作系统,其任务调度和资源分配由操作系统抽 ...

  2. Spring注解@Resource和@Autowired区别对比、spring扫描的默认bean的Id、程序获取spring容器对象

    -------------------------注解扫面的bean的ID问题-------------------------- 0.前提需要明白注解扫描出来的bean的id默认是类名首字母小写,当 ...

  3. Python第三方库jieba(中文分词)入门与进阶(官方文档)

    jieba "结巴"中文分词:做最好的 Python 中文分词组件 github:https://github.com/fxsjy/jieba 特点 支持三种分词模式: 精确模式, ...

  4. PHP路由代码

    <?php /**  * 路由  * @author 角度 QQ:1286522207  *  */ class Dispatcher extends Action {     private ...

  5. linux非阻塞的socket EAGAIN的错误处理【转】

    转自:http://blog.csdn.net/tianmohust/article/details/8691644 版权声明:本文为博主原创文章,未经博主允许不得转载. 在Linux中使用非阻塞的s ...

  6. java===java基础学习(4)---字符串操作

    java中的字符串操作和python中的大致相同,需要熟悉的就是具体操作形式. 关于具体api的使用,详见:java===字符串常用API介绍(转) package testbotoo; public ...

  7. 2017多校第7场 HDU 6128 Inverse of sum 推公式或者二次剩余

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6128 题意:给你n个数,问你有多少对i,j,满足i<j,并且1/(ai+aj)=1/ai+1/a ...

  8. VM虚拟机,Linux系统安装tools过程遇到 what is the location of the “ifconfig” program

    安装步骤: 复制到/mnt 解压文件 tar -zxvf VMwareTools-10.1.6-5214329.tar.gz 进入减压文件夹后安装 ./vmware-install.pl ... 一直 ...

  9. 海量文件查重SimHash和Minhash

    SimHash 事实上,传统比较两个文本相似性的方法,大多是将文本分词之后,转化为特征向量距离的度量,比如常见的欧氏距离.海明距离或者余弦角度等等.两两比较固然能很好地适应,但这种方法的一个最大的缺点 ...

  10. powershell常用操作

    创建文件 New-Item -path $file_path -itemtype file 创建目录 New-Item -path $dir_path -type directory 删除目录 Rem ...