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 ...
随机推荐
- Spark MLlib + maven + scala 试水~
使用SGD算法逻辑回归的垃圾邮件分类器 package com.oreilly.learningsparkexamples.scala import org.apache.spark.{SparkCo ...
- git commit 含有中文的代码,提示Warnning:Your console font probably doesn't support Unicode.......
git 提交代码是会遇到以下问题, git commit 代码时提示: Warning: Your console font probably doesn't support Unicode. If ...
- 算法学习记录-图——最短路径之Dijkstra算法
在网图中,最短路径的概论: 两顶点之间经过的边上权值之和最少的路径,并且我们称路径上的第一个顶点是源点,最后一个顶点是终点. 维基百科上面的解释: 这个算法是通过为每个顶点 v 保留目前为止所找到的从 ...
- JS(异步与单线程)
JS(异步与单线程) 题目1.同步和异步的区别是什么,试举例(例子见知识点) 区别: 1.同步会阻塞代码执行,而异步不会 2.alert 是同步,setTimeout 是异步 题目2.关于 setTi ...
- HttpServlet RequestDispatcher sendredirect和forward
Servlet的框架是由两个Java包组成:javax.servlet和javax.servlet.http. 在javax.servlet包中定义了所有的Servlet类都必须实现或扩展的的通用接口 ...
- css 元素垂直居中
通用 <div id="parent"> <div id="child">Content here</div> </d ...
- 爬虫开发python工具包介绍 (3)
本文来自网易云社区 作者:王涛 :arg str url: URL to fetch :arg str method: HTTP method, e.g. " ...
- 九度oj 题目1024:畅通工程
题目描述: 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出了有可能建设公路的若干条道 ...
- 九度oj 题目1153:括号匹配问题
题目描述: 在某个字符串(长度不超过100)中有左括号.右括号和大小写字母:规定(与常见的算数式子一样)任何一个左括号都从内到外与在它右边且距离最近的右括号匹配.写一个程序,找到无法匹配的左括号和右括 ...
- Java算法HmacSHA256不可用 - Java Algorithm HmacSHA256 not available
发布的jar包,运行后发现报错 Java Algorithm HmacSHA256 not available 百般思索,发现原生JAR包问题 解决办法: Copy sunjce_provider.j ...