Codeforces 1148C(思维)
题面
给出一个长度为n的排列a,每次可以交换序列的第i个和第j个元素,当且仅当\(2 \times |i-j| \geq n\),求一种交换方案,让序列从小到大排好序
分析
重点是考虑我们怎么把第x个数换到第i个位置上,且尽量不破坏其他数的位置
我们用序列的第1,n个数作为跳板,如果与n的距离满足条件就与n交换,否则与1交换,定义find(x)为x应该和谁交换。
1.把x与find(x)交换,此时x到了find(x)
2.find(x)与find(i)交换,此时x到了find(i)
3.find(i)与i交换,此时x到了i
我们可以发现这样交换只会对1,n的数产生交换,其他无关数的位置不变。我们只要对2~n-1的数执行上述操作,最后我们再看看a[1]是不是1,然后判断需不需要交换(1,n)即可
代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#define maxn 300005
using namespace std;
int n;
vector< pair<int,int> >ans;
int a[maxn];
int pos[maxn];//值为i的数的位置
inline int find(int x){
	if(2*(n-x)>=n) return n;
	else return 1;
}
void change(int x,int y){
	ans.push_back(make_pair(x,y));
	swap(pos[a[x]],pos[a[y]]);
	swap(a[x],a[y]);
} 
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		pos[a[i]]=i;
	}
	for(int i=2;i<n;i++){
		int t=pos[i];
		change(t,find(t));
		if(find(t)!=find(i)) change(find(t),find(i));
		change(i,find(i));
	}
	if(a[1]!=1) change(1,n);
	printf("%d\n",ans.size());
	for(auto p : ans){
		printf("%d %d\n",p.first,p.second);
	}
}
												
											Codeforces 1148C(思维)的更多相关文章
- Codeforces 424A (思维题)
		
Squats Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Statu ...
 - Codeforces 1060E(思维+贡献法)
		
https://codeforces.com/contest/1060/problem/E 题意 给一颗树,在原始的图中假如两个点连向同一个点,这两个点之间就可以连一条边,定义两点之间的长度为两点之间 ...
 - Queue CodeForces - 353D (思维dp)
		
https://codeforces.com/problemset/problem/353/D 大意:给定字符串, 每一秒, 若F在M的右侧, 则交换M与F, 求多少秒后F全在M左侧 $dp[i]$为 ...
 - codeforces 1244C (思维 or 扩展欧几里得)
		
(点击此处查看原题) 题意分析 已知 n , p , w, d ,求x , y, z的值 ,他们的关系为: x + y + z = n x * w + y * d = p 思维法 当 y < w ...
 - CodeForces - 417B (思维题)
		
Crash Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Status ...
 - CodeForces - 417A(思维题)
		
Elimination Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit ...
 - CodeForces 625A 思维
		
题意是说一个人喝酒 有两种办法 买塑料瓶的 a块钱 喝了就没了 或者是买玻璃瓶的b块钱 喝完还能卖了瓶子c块钱 求最多能喝多少瓶 在开始判断一次 a与b-c的关系 即两种方式喝酒的成本 如果a< ...
 - Vladik and Complicated Book CodeForces - 811B (思维实现)
		
Vladik had started reading a complicated book about algorithms containing n pages. To improve unders ...
 - The Contest CodeForces - 813A (思维)
		
Pasha is participating in a contest on one well-known website. This time he wants to win the contest ...
 
随机推荐
- vue实现搜索功能
			
vue实现搜索功能 template 部分 <!-- 搜索页面 --> <template> <div> <div class="goback&qu ...
 - 你的package包名有问题!
			
今天在Eclipse中运行我的Java程序中,就弹出了以下消息的窗口: 注意窗口的名字为Java Virtual Machine Launcher . Error : A JNI error has ...
 - java通过图片URL下载图片
			
public InputStream getInputStream(String imgUrl) { InputStream inputStream = null; try{ HttpURLConne ...
 - POJ-1390-Blocks (复杂区间DP)
			
$ POJ~1390~~Blocks: $ (很难想的区间DP) $ solution: $ 很好的一道题目.看起来似乎很简单,当时一直认为可以用二维区间DP来完成,转移 $ n^3 $ . 后来发现 ...
 - web编程jsp小tips
			
jsp文件头 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEn ...
 - JAVA中的反射机制 详解
			
主要介绍以下几方面内容 理解 Class 类 理解 Java 的类加载机制 学会使用 ClassLoader 进行类加载 理解反射的机制 掌握 Constructor.Method.Field 类的用 ...
 - YottaChain主网全面上线预示商业应用的落地区块链云存储不一样的云
			
Yottachain存储网12月24日平安夜全面启动,意味着全球首个可商用的区块链存储公链全面落地.对于数据存储来说,小到我们个人的照片文档,大到政企机构数据库资料,都是互联网生活中如影随形的标配. ...
 - FPGA设计中遇到的奇葩问题之“芯片也要看出身”
			
FPGA设计中遇到的奇葩问题之“芯片也要看出身”(一) 昨夜西风凋碧树.独上高楼,望尽天涯路 2000年的时候,做设计基本都是使用Xilinx公司的Virtex和Virtex-E系列芯片.那时候Alt ...
 - 【转】iis解决应用程序池**提供服务的进程意外终止进程ID是**。进程退出代码是'0x80'
			
转自:http://blog.sina.com.cn/s/blog_56a68d5501013xdd.html 我们公司旗下的红黑互联会遇到这种问题 事件类型: 警告事件来源: W3SVC事件种类: ...
 - Arithmetic Sequence
			
Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...