Codefoces 432 C. Prime Swaps
哥德巴赫猜想:
任一大于5的整数都可写成三个质数之和。
贪心取尽可能大的素数.....
2 seconds
256 megabytes
standard input
standard output
You have an array a[1], a[2], ..., a[n], containing distinct integers from 1 to n.
Your task is to sort this array in increasing order with the following operation (you may need to apply it multiple times):
- choose two indexes, i and j (1 ≤ i < j ≤ n; (j - i + 1) is
a prime number); - swap the elements on positions i and j; in other
words, you are allowed to apply the following sequence of assignments: tmp = a[i], a[i] = a[j], a[j] = tmp (tmp is
a temporary variable).
You do not need to minimize the number of used operations. However, you need to make sure that there are at most 5n operations.
The first line contains integer n (1 ≤ n ≤ 105).
The next line contains n distinct integers a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ n).
In the first line, print integer k (0 ≤ k ≤ 5n) —
the number of used operations. Next, print the operations. Each operation must be printed as "i j"
(1 ≤ i < j ≤ n; (j - i + 1) is
a prime).
If there are multiple answers, you can print any of them.
3
3 2 1
1
1 3
2
1 2
0
4
4 2 3 1
3
2 4
1 2
2 4
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int maxn=100100; bool vis[maxn];
int prime[maxn/10],pn;
int n,a[maxn],b[maxn];
int Left[maxn*5],Right[maxn*5],nu; void get_prime()
{
for(int i=2;i*i<maxn;i++)
{
for(int j=i*2;j<maxn;j+=i)
{
vis[j]=1;
}
}
for(int i=2;i<maxn;i++)
{
if(vis[i]==0)
prime[pn++]=i;
}
} int Bin(int x)
{
int low=0,high=pn-1,mid,ans=-1;
while(low<=high)
{
mid=(low+high)/2;
if(prime[mid]<=x)
{
ans=prime[mid],low=mid+1;
}
else high=mid-1;
}
return ans;
} void debug()
{
cout<<"a....\n"; for(int i=1;i<=n;i++) cout<<a[i]<<","; cout<<endl;
cout<<"b....\n"; for(int i=1;i<=n;i++) cout<<b[i]<<","; cout<<endl;
} int main()
{
get_prime();
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",a+i);
b[a[i]]=i;
}
for(int i=1;i<=n;i++)
{
/// from b[i] to i greedy!!!
int len=b[i]-i+1;
while(len!=1)
{
int bin=Bin(len);
int cp=b[i]-bin+1; /// changepos
Left[nu]=cp,Right[nu]=b[i]; nu++;
int t1=b[i],t2=b[a[cp]];
swap(a[cp],a[b[i]]);
b[a[b[i]]]=t1;b[i]=t2;
// debug(); getchar();
len=b[i]-i+1;
}
}
printf("%d\n",nu);
for(int i=0;i<nu;i++)
{
printf("%d %d\n",Left[i],Right[i]);
}
return 0;
}
版权声明:来自: 代码代码猿猿AC路 http://blog.csdn.net/ck_boss
Codefoces 432 C. Prime Swaps的更多相关文章
- Codefoces 432 C. Prime Swaps(水)
思路:从前往后想将1调整好,在调整2....这样平均每次有五次机会调整,并且有相当一部分可能都用不到五次,能够一试.ac 代码: #include<iostream> #include&l ...
- Codefoces 432C Prime Swaps(数论+贪心)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/26094917 题目连接:Codefoces ...
- CodeForces 432C Prime Swaps
Description You have an array a[1], a[2], ..., a[n], containing distinct integers from 1 to n. Your ...
- Codeforces Round #246 (Div. 2) C. Prime Swaps(贪心,数论)
题目链接:http://codeforces.com/contest/432/problem/C 首先由题意分析出:这些数是从1到n且各不相同,所以最后结果肯定是第i位的数就是i. 采用这样一种贪心策 ...
- codeforces C. Prime Swaps
题意:给你n个数,然后在交换次数小于等于5×n的情况下使得这个序列变成升序,输出次数; 思路:哥德巴赫猜想:任何一个大于5的数都可以写成三个质数之和.尽可能的找大的素数,从1的位置向右逐步的调整,每一 ...
- Prime Factory
Your task is simple:Find the first two primes above 1 million, whose separate digit sums are also pr ...
- Codeforces #432 Div2 D
#432 Div2 D 题意 给出一些数字,如果这些数字的的 \(gcd\) 不为1则称这些数字 \(good\). 可以有两种操作: 花费 x 删掉一个数 花费 y 将一个数加 1 问使这些数 \( ...
- POJ 3126:Prime Path
Prime Path Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit St ...
- Java 素数 prime numbers-LeetCode 204
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
随机推荐
- js检测浏览器中是否安装了flash播放插件
这两天工作中需要在网页中嵌入flash小游戏,我使用的是swfobject.js version:1.5.其他方面都很好,唯独版本检测这里一直没有搞通,后来实在无奈之下,改用js来检测浏览器的flas ...
- oracle之spool详细使用总结(转)
今天实际项目中用到了spool,发现网上好多内容不是很全,自己摸索了好半天,现在总结一下. 一.通过spool 命令,可以将select 数据库的内容写到文件中,通过在sqlplus设置一些参数,使得 ...
- Java集合类汇总记录-- apache.commons4(TreeList)
通常.Tree是Tree,List是List,两者不太可能混在一起.但apache-commons库却用tree实现了实现了List的接口,也就是TreeList类.与标准的LinkedList相比. ...
- hbase列表排序
hbase都是依照字典序进行排序的,也就是降序,在页面的表现就是最早的数据(rowkey最小的)排在前面. 眼下的解决方式是:给主键添加一个外键关联表.外键的生成规则是 400000000000-主键 ...
- 内网port映射具体解释(花生壳)
关于怎样建立服务器的解答. 一.花生壳的作用 首先,我们先来了解一下花生壳的究竟有什么作用.由于ADSL每次拨号上网所获得的IP地址每次都是不同的,花生壳起到的作用就是方便用户訪问我们的server( ...
- Windows Phone开发(4):框架和页
原文:Windows Phone开发(4):框架和页 在开如之前,我想更正一个小问题,之前我在第一篇文章中说,Visual Studio Express for Windows Phone的中文版没有 ...
- Win32 Windows规划 三
一.NMAKE 和 Makefile 1.1 NMAKE - 命令解释器. 依据Makefile文件里定义的脚本.完毕项目的编译等操作 1.2 Makefile - 定义编译.连接等脚本语言 1.3 ...
- 冒泡排序算法(Java)
冒泡排序即每次遍历.相邻数字间进行比較,前者大于后者进行交换,不断将最大值后移,直至沉至最后位置:算法关键要点在于确定每次循环的边界. 后面两种算法则是对冒泡排序一定程度上的改良,但相对于其它排 ...
- Morphia采用
Morphia配合mongoDB喜欢Hibernate关系型数据库.也能够想到Morphia于MongoDB它调用上述的基础上,mongoDB语法. 于java直接通话Morphia的api它会变得更 ...
- Android使得手机拍照功能的发展(源共享)
Android系统调用手机拍照功能有两种方法来直接调用手机自带摄像头还有一个就是要当心自己的节拍. 例Camera360 强大的一个在每个操作系统都有一个手机摄影软件:您可以捕捉不同风格,不同特效的照 ...