Codefoces 432 C. Prime Swaps(水)
来源:互联网 发布:c语言continue for 编辑:程序博客网 时间:2024/05/12 21:48
思路:从前往后想将1调整好,在调整2。。。。这样平均每次有五次机会调整,而且有相当一部分可能都用不到五次,可以一试。ac
代码:
#include<iostream>#include<cstdio>#include<cmath>#include<map>#include<queue>#include<cstring>#include<algorithm>using namespace std;const int maxn=600005;const int maxm=100005;int P[maxm],p[maxm];int u[maxn],v[maxn],da[maxn],pos[maxn];void init(){ for(int i=2;i<maxm;i++) if(!P[i]) for(int j=i*i;i<1000&&j<maxm;j+=i) P[j]=1;}int main(){ int n,cnt=0; init(); for(int i=2;i<maxm;i++) if(!P[i])p[cnt++]=i; while(~scanf("%d",&n)) { int scnt=0; for(int i=1;i<=n;i++) scanf("%d",&da[i]),pos[da[i]]=i; for(int i=1;i<=n;i++) { while(da[i]!=i) { int t=upper_bound(p,p+cnt,pos[i]-i+1)-p; t--; int tnp=pos[i],tmp=pos[i]-p[t]+1; int tm=da[tnp]; da[tnp]=da[tmp]; da[tmp]=tm; tm=pos[da[tnp]]; pos[da[tnp]]=pos[da[tmp]]; pos[da[tmp]]=tm; u[scnt]=tnp;v[scnt++]=tmp; } } printf("%d\n",scnt); for(int i=0;i<scnt;i++) printf("%d %d\n",v[i],u[i]); } return 0;}
0 0
- Codefoces 432 C. Prime Swaps(水)
- Codefoces 432 C. Prime Swaps
- Codefoces 432C Prime Swaps(数论+贪心)
- 432 C. Prime Swaps
- codeforces 432c Prime Swaps
- codeforces 432C Prime Swaps
- Codeforces 432C Prime Swaps
- CodeForces 432C - Prime Swaps (数论 + 贪心)
- Codeforces Round #246 (Div. 2) 432C Prime Swaps(脑洞)
- Codeforces 432C Prime Swaps【筛法素数预处理+贪心】
- Prime Swaps
- Codeforces Round #246 (Div. 2)C. Prime Swaps
- Codeforces Round #246 (Div. 2) C. Prime Swaps
- Codeforces Round #246 (Div. 2)-C. Prime Swaps
- codefoces 581C Developing Skills(贪心)
- 【贪心】Codefoces 486C
- codefoces 597C - Subsequences
- Codefoces 828C
- HiPAC高性能规则匹配算法之查找过程
- [ffmpeg] build symbol file
- 【算法学习-01】 输入一个正数 n,输出所有和为 n 连续正数序列。
- C++模板递归1
- ubuntu安装ROS/turtlebot
- Codefoces 432 C. Prime Swaps(水)
- Android 关于双缓冲画图
- JAVA 服务提供者框架介绍
- 关键路径
- Alaska
- c struct 内存分配
- 浅谈寄存器的知识
- Bloom Filter存在的意义
- IOS奔溃及其调试总结