题意:给你n个数,然后在交换次数小于等于5×n的情况下使得这个序列变成升序,输出次数;

思路:哥德巴赫猜想:任何一个大于5的数都可以写成三个质数之和。尽可能的找大的素数,从1的位置向右逐步的调整,每一个位置最多5次,有的位置不到5次;

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define maxn 100010
using namespace std; int n;
int a[maxn+];
bool vis[maxn+];
int f[maxn+];
int p[maxn];
int s[maxn*],e[maxn*];
int cnt=;
int bs(int x,int l,int r)
{
int pos=;
while(l<r)
{
int mid=(l+r)/;
if(f[mid]>x)
{
r=mid;
}
else
{
l=mid+;
pos=l;
}
}
return pos;
}
void get_prime()
{
vis[]=true;
vis[]=true;
for(int i=; i<maxn; i++)
{
if(!vis[i])
{
for(int j=i*; j<maxn; j+=i)
{
vis[j]=true;
}
}
}
for(int i=; i<maxn; i++)
{
if(!vis[i])
{
f[cnt++]=i;
}
}
} int main()
{
get_prime();
scanf("%d",&n);
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
p[a[i]]=i;
}
int ans=;
for(int i=; i<=n; i++)
{
while(a[i]!=i)
{
int t=bs(p[i]-i+,,cnt-);
t--;
int sx=p[i],ex=p[i]-f[t]+;
swap(a[sx],a[ex]);
swap(p[a[sx]],p[a[ex]]);
s[ans]=sx; e[ans++]=ex;
}
}
printf("%d\n",ans);
for(int i=; i<ans; i++)
{
printf("%d %d\n",e[i],s[i]);
}
return ;
}

codeforces C. Prime Swaps的更多相关文章

  1. CodeForces 432C Prime Swaps

    Description You have an array a[1], a[2], ..., a[n], containing distinct integers from 1 to n. Your ...

  2. Codefoces 432 C. Prime Swaps

    哥德巴赫猜想: 任一大于2的偶数,都可表示成两个素数之和. 任一大于5的整数都可写成三个质数之和. 贪心取尽可能大的素数..... C. Prime Swaps time limit per test ...

  3. Codefoces 432C Prime Swaps(数论+贪心)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/26094917 题目连接:Codefoces ...

  4. Codeforces H. Prime Gift(折半枚举二分)

    题目描述: Prime Gift time limit per test 3.5 seconds memory limit per test 256 megabytes input standard ...

  5. Codeforces Round #246 (Div. 2) C. Prime Swaps(贪心,数论)

    题目链接:http://codeforces.com/contest/432/problem/C 首先由题意分析出:这些数是从1到n且各不相同,所以最后结果肯定是第i位的数就是i. 采用这样一种贪心策 ...

  6. CodeForces 691D:Swaps in Permutation(并查集)

    http://codeforces.com/contest/691/problem/D D. Swaps in Permutation   You are given a permutation of ...

  7. [Codeforces 1178D]Prime Graph (思维+数学)

    Codeforces 1178D (思维+数学) 题面 给出正整数n(不一定是质数),构造一个边数为质数的无向连通图(无自环重边),且图的每个节点的度数为质数 分析 我们先构造一个环,每个点的度数都是 ...

  8. Codeforces 912E - Prime Gift

    912E - Prime Gift 思路: 折半枚举+二分check 将素数分成两个集合(最好按奇偶位置来,保证两集合个数相近),这样每个集合枚举出来的小于1e18的积个数小于1e6. 然后二分答案, ...

  9. Codeforces 912E Prime Gift(预处理 + 双指针 + 二分答案)

    题目链接 Prime Gift 题意  给定一个素数集合,求第k小的数,满足这个数的所有质因子集合为给定的集合的子集. 保证答案不超过$10^{18}$ 考虑二分答案. 根据折半的思想,首先我们把这个 ...

随机推荐

  1. MySQL slave状态之Seconds_Behind_Master

    在MySQL的主从环境中,我们能够通过在slave上运行show slave status来查看slave的一些状态信息,当中有一个比較重要的參数Seconds_Behind_Master.那么你是否 ...

  2. JSON 入门

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族 ...

  3. 查询可用的Nuget服务地址

    解决访问Nuget源失败问题 查询IP址址 nslookup nuget.org 如失败,通过google 的dns服务器查询 nslookup nuget.org 8.8.8.8 将得到的Ip地址加 ...

  4. move file create directory.

    If we want to move file to the directory that does not exist,and if we perform a File.Move,it will r ...

  5. clientX 属性.

    Syntax: event.clientX The clientX event attribute returns the horizontal coordinate (according to th ...

  6. C#中的Dictionary简介

    简介在C#中,Dictionary提供快速的基于兼职的元素查找.当你有很多元素的时候可以使用它.它包含在System.Collections.Generic名空间中. 在使用前,你必须声明它的键类型和 ...

  7. 【原创】不用封装jar包 直接引入工程使用的方法(类似android的 is Library功能)

    1.制作lib工程,这里我简单制作一个测试类 2.eclipse中 java Project工程引入方法 2.1.新建个java工程,在属性配置中选择 "Java Build Path&qu ...

  8. ssh配置事务

    http://blog.csdn.net/jianxin1009/article/details/9202907(不错)

  9. 转: Oracle AWR 报告 每天自动生成并发送邮箱

    原贴地址:http://www.cnblogs.com/vigarbuaa/archive/2012/09/05/2671794.html Oracle AWR 介绍http://blog.csdn. ...

  10. (whh仅供自己参考)进行ip网络请求的步骤

    这个过程大致是这个样子: 1 添加通知 2 发送网络请求 里边有一个发送通知的操作 3 执行发送通知的具体操作 代码如下: 1 在VC添加通知 [[NSNotificationCenter defau ...