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 ...
随机推荐
- 【ORACLE】调整序列的当前种子值
[ORACLE]调整序列的当前种子值 --必须用SYS用户执行脚本:或具有SYSDBA角色登录: CREATE OR replace ); v_step ):;--步进 tsql ); BEGIN E ...
- 【11】specified value,computed value,used value计算方法
[11]specified value,computed value,used value计算方法 specified value(规范值): 计算方法如下: 如果样式表设置了一个值,使用这个值 如果 ...
- jquery 页面加载效果
30个jquery 页面加载效果 30个jquery 页面加载效果 30 CSS Page Preload Animations 加载效果列表 Square Animations Demo 1 ...
- A. Nearest Common Ancestors
A. Nearest Common Ancestors Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 10000KB 64-bi ...
- Java的反射机制和动态代理
介绍Java注解的时候,多次提到了Java的反射API.与javax.lang.model不同的是,通过反射API可以获取程序在运行时刻的内部结构.反射API中提供的动态代理也是非常强大的功能,可以原 ...
- Count on a tree(bzoj 2588)
Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...
- Iptables入门教程
转自:http://drops.wooyun.org/tips/1424 linux的包过滤功能,即linux防火墙,它由netfilter 和 iptables 两个组件组成. netfilter ...
- SPFA的两个(卡时)优化
SPFA算法有两个优化算法 SLF 和 LLL: SLF:Small Label First 策略,设要加入的节点是j,队首元素为i,若dist(j)<dist(i),则将j插入队首,否则插入队 ...
- java面2
面试试题汇总集: <[面试题]2018年最全Java面试通关秘籍汇总集!> <[面试题]2018年最全Java面试通关秘籍第二套!> <[面试题]2018年最全Java面 ...
- laravel 数据库配置
数据库配置文件为项目根目录下的config/database.php //默认数据库为mysql 'default' => env('DB_CONNECTION', 'mysql'), 'mys ...