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 ...
随机推荐
- “程序设计与算法训练”课程设计:“BP神经网络的实现”(C++类封装实现)
一 题目: 71 BP神经网络的实现: 利用C++语言实现BP神经网络, 并利用BP神经网络解决螨虫分类问题: 蠓虫分类问题:对两种蠓虫(A与B)进行鉴别,依据的资料是触角和翅膀的长度,已知了9支Af ...
- python基础学习笔记——shelve、shutil模块
shelve 我们之前学了json和pickle模块 这些都是序列化的模块,咱们进行在讲一个序列化的东西 叫做shelve 你们肯定有个疑问,这个东西和那个类似为什么要讲.是因为这个模块比较简单的,并 ...
- python基础学习笔记——生成器与推导式
生成器 首先我们来看看什么是个生成器,生成器本质就是迭代器 在python中有三种方式来获取生成器 1.通过生成器函数 2.通过各种推到式来实现生成器 3.通过数据的转换也可以获取生成器 首先,我们先 ...
- search Paths $(SRCROOT)和$(PROJECT_DIR)区别
$(SRCROOT)代表的时项目根目录下 $(PROJECT_DIR)代表的是整个项目 PS:往项目添加文件时,例如.a等,要先showinfinder ,复制到项目中,然后再拖到xcode项目中
- (总结)CentOS Linux使用crontab运行定时任务详解
安装crontab:yum install crontabs 说明:/sbin/service crond start //启动服务/sbin/service crond stop //关闭服务/sb ...
- 【机房收费系统 4】:VB获取标准北京时间,免除时间误差
导读:这又是师傅给我指出的一个问题,说实话,其实开始根本没有当回事,觉得麻烦,可是,等我完成了获取标准北京时间后,我发现,这一步,是必须的.谢谢师傅对我的严格要求,让我一步一步的成长起来! 一.事件缘 ...
- 【Luogu】P1948电话线(二分SPFA)
题目链接 二分最长的电话线长度.把所有大于这个长度的边权设成1,小于等于的设成零,然后跑SPFA看dis[n]是否>k.若>k则l=mid+1 否则r=mid-1 放代码 #include ...
- BZOJ 2693 jzptab ——莫比乌斯反演
同BZOJ 2154 但是需要优化 $ans=\sum_{d<=n}d*\sum_{i<=\lfloor n/d \rfloor} i^2 *\mu(i)* Sum(\lfloor \fr ...
- java解决前后台跨域问题
这篇文章主要介绍了使用Cors实现JavaWeb跨域请求问题的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下 之前用jsonp 解决跨域问题,现在用CORS实现跨域请求解决java 跨域问题: ...
- [暑假集训--数位dp]hdu3652 B-number
A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- ...