【题目链接】 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. Spring4+SpringMVC+MyBatis整合思路(山东数漫江湖)

    1.Spring框架的搭建 这个很简单,只需要web容器中注册org.springframework.web.context.ContextLoaderListener,并指定spring加载配置文件 ...

  2. POJ 2456 Aggressive cows ( 二分搜索)

    题目链接 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The ...

  3. 启动Eclipse时,弹出failed to load the jni shared library

    JDK版本和Eclipse版本不同的问题,JDK版本为64位,Eclipse版本为32位.

  4. Tomcat8配置默认项目

    <!-- 配置默认访问项目 --> <Host name="localhost" appBase="webapps" unpackWARs=& ...

  5. python urllib2练习发送简单post

    import urllib2 import urllib url = 'http://localhost/1.php' while True: data = raw_input('(ctrl+c ex ...

  6. HTML综合实例【月福首页】

    1. 首页的布局结构 2. 排版的准备工作 网站的素材:与当前网页放在一起. 创建一个html文件,来进行排版. 网页的背景色 确定主页的宽度:778px 3. HTML注释 <!-- 注释内容 ...

  7. 2017多校第9场 HDU 6162 Ch’s gift 树剖加主席树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6162 题意:给出一棵树的链接方法,每个点都有一个数字,询问U->V节点经过所有路径中l < ...

  8. 关于ueditor在Java中文件上传问题,404问题

    问题困扰了两天,部署要求导入到webcontent下,我导入到了整个项目目录下,自己粗心犯错,导致页面访问不到404. 解决了上面的问题,试着进行文件上传,却一直找不到图片: 调出浏览器控制台: 刚开 ...

  9. 阻止父类的create,是无法阻止的

  10. Python+Selenium 自动化实现实例-打开浏览器模拟进行搜索数据并验证

    #导入模块 from selenium import webdriverfrom selenium.webdriver.common.keys import Keys #启动火狐浏览器driver = ...