Codefoces 432 C. Prime Swaps(水)
思路:从前往后想将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;
- }
Codefoces 432 C. Prime Swaps(水)的更多相关文章
- Codefoces 432 C. Prime Swaps
哥德巴赫猜想: 任一大于2的偶数,都可表示成两个素数之和. 任一大于5的整数都可写成三个质数之和. 贪心取尽可能大的素数..... C. Prime Swaps time limit per test ...
- Codefoces 432C Prime Swaps(数论+贪心)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/26094917 题目连接:Codefoces ...
- POJ.2739 Sum of Consecutive Prime Numbers(水)
POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...
- UESTC--1272--Final Pan's prime numbers(水题)
Final Pan's prime numbers Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format: %lld & % ...
- 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 #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 ...
- UVA 10200 Prime Time 水
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- Codeforces Round #246 (Div. 2) C. Prime Swaps(贪心,数论)
题目链接:http://codeforces.com/contest/432/problem/C 首先由题意分析出:这些数是从1到n且各不相同,所以最后结果肯定是第i位的数就是i. 采用这样一种贪心策 ...
- UVa 1644 Prime Gap (水题,暴力)
题意:给定一个数 n,求它后一个素数和前一个素数差. 析:先打表,再二分查找. 代码如下: #pragma comment(linker, "/STACK:1024000000,102400 ...
随机推荐
- MFC下拉框
在函数OnInitDialog()中添加一下语句可以添加选项到下拉框中 m_comboBox.AddString(_T("ALKATIP Basma Tom")); m_combo ...
- WCF学习-协议绑定
文章:无废话WCF入门教程三[WCF的宿主] 讲了net.tcp协议的wcf绑定.
- Java&Android代码规范
项目中直接导入Square的代码风格文件.(不导入Google的原因是Square同时提供了Java和Android两套统一风格,Google只提供了一套) Square Code Styles Go ...
- UITableView加载几种不同的cell
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- 【bzoj4200】[Noi2015]小园丁与老司机 STL-map+dp+有上下界最小流
题目描述 小园丁 Mr. S 负责看管一片田野,田野可以看作一个二维平面.田野上有 nn 棵许愿树,编号 1,2,3,…,n1,2,3,…,n,每棵树可以看作平面上的一个点,其中第 ii 棵树 (1≤ ...
- SPOJ GSS7 Can you answer these queries VII ——树链剖分 线段树
[题目分析] 问题放到了树上,直接链剖+线段树搞一搞. 调了300行+. (还是码力不够) [代码] #include <cstdio> #include <cstring> ...
- [BZOJ1592] [Usaco2008 Feb]Making the Grade 路面修整(DP)
传送门 有个结论,每一个位置修改高度后的数,一定是原来在这个数列中出现过的数 因为最终结果要么不递增要么不递减, 不递增的话, 如果x1 >= x2那么不用动,如果x1 < x2,把x1变 ...
- 小机房的树(codevs 2370)
题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号为0到N-1,有两只虫子名叫飘狗和大吉狗,分居在两个不同的节点上.有一天,他们想爬到一个节点上去搞基,但是作为两只虫子, ...
- [NOIP2009] 提高组 洛谷P1072 Hankson 的趣味题
题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现 在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课堂上,老师讲 ...
- Linux下AT&T汇编语法格式与Intel汇编语法格式异同
由于绝大多数的国内程序员以前只接触过Intel格式的汇编语言,很少或几乎没有接触过AT&T汇编语言,虽然这些汇编代码都是Intel风格的.但在Unix和Linux系统中,更多采用的还是AT&a ...