思路:从前往后想将1调整好,在调整2。。。。这样平均每次有五次机会调整,并且有相当一部分可能都用不到五次,能够一试。ac

代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<map>
  5. #include<queue>
  6. #include<cstring>
  7. #include<algorithm>
  8. using namespace std;
  9.  
  10. const int maxn=600005;
  11. const int maxm=100005;
  12. int P[maxm],p[maxm];
  13. int u[maxn],v[maxn],da[maxn],pos[maxn];
  14. void init()
  15. {
  16. for(int i=2;i<maxm;i++)
  17. if(!P[i])
  18. for(int j=i*i;i<1000&&j<maxm;j+=i)
  19. P[j]=1;
  20. }
  21. int main()
  22. {
  23. int n,cnt=0;
  24. init();
  25. for(int i=2;i<maxm;i++)
  26. if(!P[i])p[cnt++]=i;
  27. while(~scanf("%d",&n))
  28. {
  29. int scnt=0;
  30. for(int i=1;i<=n;i++)
  31. scanf("%d",&da[i]),pos[da[i]]=i;
  32. for(int i=1;i<=n;i++)
  33. {
  34. while(da[i]!=i)
  35. {
  36. int t=upper_bound(p,p+cnt,pos[i]-i+1)-p;
  37. t--;
  38. int tnp=pos[i],tmp=pos[i]-p[t]+1;
  39. int tm=da[tnp];
  40. da[tnp]=da[tmp];
  41. da[tmp]=tm;
  42. tm=pos[da[tnp]];
  43. pos[da[tnp]]=pos[da[tmp]];
  44. pos[da[tmp]]=tm;
  45. u[scnt]=tnp;v[scnt++]=tmp;
  46. }
  47. }
  48. printf("%d\n",scnt);
  49. for(int i=0;i<scnt;i++)
  50. printf("%d %d\n",v[i],u[i]);
  51. }
  52. return 0;
  53. }

Codefoces 432 C. Prime Swaps(水)的更多相关文章

  1. Codefoces 432 C. Prime Swaps

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

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

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

  3. POJ.2739 Sum of Consecutive Prime Numbers(水)

    POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...

  4. UESTC--1272--Final Pan's prime numbers(水题)

    Final Pan's prime numbers Time Limit: 1000MS   Memory Limit: 65535KB   64bit IO Format: %lld & % ...

  5. CodeForces 432C Prime Swaps

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

  6. Codeforces Round #356 (Div. 2) C. Bear and Prime 100 水题

    C. Bear and Prime 100 题目连接: http://www.codeforces.com/contest/680/problem/C Description This is an i ...

  7. UVA 10200 Prime Time 水

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

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

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

  9. UVa 1644 Prime Gap (水题,暴力)

    题意:给定一个数 n,求它后一个素数和前一个素数差. 析:先打表,再二分查找. 代码如下: #pragma comment(linker, "/STACK:1024000000,102400 ...

随机推荐

  1. MFC下拉框

    在函数OnInitDialog()中添加一下语句可以添加选项到下拉框中 m_comboBox.AddString(_T("ALKATIP Basma Tom")); m_combo ...

  2. WCF学习-协议绑定

    文章:无废话WCF入门教程三[WCF的宿主] 讲了net.tcp协议的wcf绑定.

  3. Java&Android代码规范

    项目中直接导入Square的代码风格文件.(不导入Google的原因是Square同时提供了Java和Android两套统一风格,Google只提供了一套) Square Code Styles Go ...

  4. UITableView加载几种不同的cell

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  5. 【bzoj4200】[Noi2015]小园丁与老司机 STL-map+dp+有上下界最小流

    题目描述 小园丁 Mr. S 负责看管一片田野,田野可以看作一个二维平面.田野上有 nn 棵许愿树,编号 1,2,3,…,n1,2,3,…,n,每棵树可以看作平面上的一个点,其中第 ii 棵树 (1≤ ...

  6. SPOJ GSS7 Can you answer these queries VII ——树链剖分 线段树

    [题目分析] 问题放到了树上,直接链剖+线段树搞一搞. 调了300行+. (还是码力不够) [代码] #include <cstdio> #include <cstring> ...

  7. [BZOJ1592] [Usaco2008 Feb]Making the Grade 路面修整(DP)

    传送门 有个结论,每一个位置修改高度后的数,一定是原来在这个数列中出现过的数 因为最终结果要么不递增要么不递减, 不递增的话, 如果x1 >= x2那么不用动,如果x1 < x2,把x1变 ...

  8. 小机房的树(codevs 2370)

    题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号为0到N-1,有两只虫子名叫飘狗和大吉狗,分居在两个不同的节点上.有一天,他们想爬到一个节点上去搞基,但是作为两只虫子, ...

  9. [NOIP2009] 提高组 洛谷P1072 Hankson 的趣味题

    题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现 在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课堂上,老师讲 ...

  10. Linux下AT&T汇编语法格式与Intel汇编语法格式异同

    由于绝大多数的国内程序员以前只接触过Intel格式的汇编语言,很少或几乎没有接触过AT&T汇编语言,虽然这些汇编代码都是Intel风格的.但在Unix和Linux系统中,更多采用的还是AT&a ...