Codeforces 804E The same permutation(构造)
【题目链接】 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(构造)的更多相关文章
- codeforces 622C. Optimal Number Permutation 构造
题目链接 假设始终可以找到一种状态使得值为0, 那么两个1之间需要隔n-2个数, 两个2之间需要隔n-3个数, 两个3之间隔n-4个数. 我们发现两个三可以放到两个1之间, 同理两个5放到两个3之间. ...
- Educational Codeforces Round 7 D. Optimal Number Permutation 构造题
D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...
- 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 ...
- 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/ ...
- Codeforces 482 - Diverse Permutation 构造题
这是一道蛮基础的构造题. - k +(k - 1) -(k - 2) 1 + k , 1 , k , 2, ....... ...
- codeforces C. Diverse Permutation(构造)
题意:1...n 的全排列中 p1, p2, p3....pn中,找到至少有k个 |p1-p2| , |p2-p3|, ...|pn-1 - pn| 互不相同的元素! 思路: 保证相邻的两个数的差值的 ...
- Codeforces.612E.Square Root of Permutation(构造)
题目链接 \(Description\) 给定一个\(n\)的排列\(p_i\),求一个排列\(q_i\),使得对于任意\(1\leq i\leq n\),\(q_{q_i}=p_i\).无解输出\( ...
- codeforces B. Levko and Permutation 解题报告
题目链接:http://codeforces.com/problemset/problem/361/B 题目意思:有n个数,这些数的范围是[1,n],并且每个数都是不相同的.你需要构造一个排列,使得这 ...
- Codeforces 691D Swaps in Permutation
Time Limit:5000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Status Prac ...
随机推荐
- 【洛谷 P4320】 道路相遇 (圆方树,LCA)
题目链接 题意:给一张无向图和\(M\)个询问,问\(u,v\)之间的路径的必经之点的个数. 对图建出圆方树,然后必经之点就是两点路径经过的原点个数,用\((dep[u]+dep[v]-dep[LCA ...
- java 连接数据库报错:Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '
1.解决方法: 报错信息为: Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server ti ...
- html+js实现的触屏版贪吃蛇
查看线上demo(服务器经常断开,推荐下载源码本地打开): http://47.93.103.19:8044/client/ ; 使用手机打开或者chrome浏览器的手机模式打开 源码地址 :http ...
- Winform GDI+
什么是GDI+ GDI (Graphics Device Interface), 是属于绘图方面的 API (Application Programming Interface). 因为应用程序不能直 ...
- Javascript prototype 及 继承机制的设计思想
我一直很难理解Javascript语言的继承机制. 它没有"子类"和"父类"的概念,也没有"类"(class)和"实例" ...
- C++学习之路(八):关于C++提供的强制类型转换
C语言中提供了旧式的强制类型转换方法.比如: int a =1; char *p = (char *)&a; 上述将a的地址单元强制转换为char类型的指针.这里暂且不说上述转换结果是否合理 ...
- 单从软件层面来说,Maya 和 Blender 差别在哪?
单从软件层面来说,Maya 和 Blender 差别在哪? https://www.zhihu.com/question/21975571
- 经典卷积网络模型 — VGGNet模型笔记
一.简介 VGGNet是计算机视觉组(Visual Geometry Group)和Google DeepMind公司的研究员一起研究的深度卷积神经网络.VGGNet探索了卷积神经网络深度与性能之间的 ...
- memcached结合php以及memcache共享session
//安装php的memcache扩展 一.使用php自带的pecl安装程序 [root@localhost src]# /usr/local/php/bin/pecl install memcache ...
- 解决: httpclient ssl 验证导致死锁问题
线上图片下载服务器平时运行正常,最近突然出现一种比较奇怪的现象,只接受请求,但却没有处理请求,最开始怀疑下载线程挂掉了,dump 项目线程后发现异常: "pool-2-thread-1&qu ...