题面

给出一个长度为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(思维)的更多相关文章

  1. Codeforces 424A (思维题)

    Squats Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Statu ...

  2. Codeforces 1060E(思维+贡献法)

    https://codeforces.com/contest/1060/problem/E 题意 给一颗树,在原始的图中假如两个点连向同一个点,这两个点之间就可以连一条边,定义两点之间的长度为两点之间 ...

  3. Queue CodeForces - 353D (思维dp)

    https://codeforces.com/problemset/problem/353/D 大意:给定字符串, 每一秒, 若F在M的右侧, 则交换M与F, 求多少秒后F全在M左侧 $dp[i]$为 ...

  4. codeforces 1244C (思维 or 扩展欧几里得)

    (点击此处查看原题) 题意分析 已知 n , p , w, d ,求x , y, z的值 ,他们的关系为: x + y + z = n x * w + y * d = p 思维法 当 y < w ...

  5. CodeForces - 417B (思维题)

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

  6. CodeForces - 417A(思维题)

    Elimination Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit  ...

  7. CodeForces 625A 思维

    题意是说一个人喝酒 有两种办法 买塑料瓶的 a块钱 喝了就没了 或者是买玻璃瓶的b块钱 喝完还能卖了瓶子c块钱 求最多能喝多少瓶 在开始判断一次 a与b-c的关系 即两种方式喝酒的成本 如果a< ...

  8. Vladik and Complicated Book CodeForces - 811B (思维实现)

    Vladik had started reading a complicated book about algorithms containing n pages. To improve unders ...

  9. The Contest CodeForces - 813A (思维)

    Pasha is participating in a contest on one well-known website. This time he wants to win the contest ...

随机推荐

  1. 使用vue-resource请求数据的步骤

    1.需要安装 vue-resource模块 注意加上--save npm install vue-resource --save 2.main.js 引入vue-resource import Vue ...

  2. c# 匿名委托

    using System; namespace AnonymousMethod { delegate void ArithmeticOperation(double operand1, double ...

  3. 前端每日实战:98# 视频演示如何用纯 CSS 创作一只愤怒小鸟中的绿猪

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/VBGWqX 可交互视频 此视频是可 ...

  4. uiautomator2 使用注意的地方

    uiautomator2项目地址:https://github.com/openatx/uiautomator2#basic-api-usages 下面记录一些自己在使用过程中的坑,仅供参考 1.通过 ...

  5. zabbix4.0 percona插件实现监控mysql

    percona是一款能够详细监控zabbix MySQL的插件 官方下载percona插件 wget https://www.percona.com/downloads/percona-monitor ...

  6. linux nginx管理

    1.添加 Nginx 服务 vim /lib/systemd/system/nginx.service 添加如下内容: [Unit]Description=nginxAfter=network.tar ...

  7. char* 和 cha[]

    char* s1 = "hello";//字符串常量 s是一个保存了字符串首地址的指针变量,同时也是字符串的名字,s的内容是第一个字符的地址,当s指向常量字符串时候,内容不能改变( ...

  8. 20180822-Java接口

    Java 接口 接口(英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明.一个类通过继承接口的方式,从而来继承接口的抽象方法. 接口并 ...

  9. 「THUPC 2017」机场 / Airport

    https://loj.ac/problem/2403 题解 神仙题. 练习赛的时候想了个假建图. 正解太神仙了. 先把不合法情况判掉. 先对时间离散化,每个时间点开一个点. 然后把他们一次串起来,中 ...

  10. [CSP-S模拟测试]:Walk(树的直径+数学)

    题目描述 给定一棵$n$个节点的树,每条边的长度为$1$,同时有一个权值$w$.定义一条路径的权值为路径上所有边的权值的最大公约数.现在对于任意$i\in [1,n]$,求树上所有长度为$i$的简单路 ...