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 ...
随机推荐
- C++类设计1(Class without pointer members)
class complex{ public: complex (double r = 0, double i = 0):re(r), im(i){} //inline complex& ope ...
- Python第三方库之openpyxl(7)
Python第三方库之openpyxl(7) 散点图 散点或xy图表类似于一些折线图.主要的区别在于,一个系列的值被绘制在另一个值上.当值未排序时,这是有用的. from openpyxl impor ...
- B. Balanced Lineup
B. Balanced Lineup Time Limit: 5000ms Case Time Limit: 5000ms Memory Limit: 65536KB 64-bit integer ...
- Codeforces Round #390 (Div. 2) A+B+D!
A. Lesha and array splitting 水题模拟.(0:10) 题意:给你一个n个元素的数组,求能否把这个数组分成若干连续小段,使得每段的和不为0.如有多种解输出任意一个. 思路:搞 ...
- 全库修改SQL Server现有排序规则
近日,在项目Debug过程中发现了SQL Server排序规则冲突的问题. 由于原数据库是从英文环境的SQL中生成的,其排序规则为“SQL_Latin1_General_CP1_CI_AS”,备份到本 ...
- 也来“玩”Metro UI之磁贴(二)
继昨天的“也来“玩”Metro UI之磁贴(一)”之后,还不过瘾,今天继续“玩”吧——今天把单选的功能加进来,还有磁贴的内容,还加了发光效果(CSS3,IE9+浏览器),当然,还是纯CSS,真的要感谢 ...
- Redis常见配置redis.conf
redis的配置文件.相信学过SSH或SSM的读者都知道,配置文件的使用在当下开发已十分普遍,希望大家要熟悉习惯这 种开发方式,废话不多说,来开始我们今天的内容吧. 首先得找到 redis 的配置文件 ...
- 【2018.10.20】CXM笔记(思维)
1. 给你个环状字符串,问从哪个地方拆开能使它的字典序最小. 先预处理任意子串的哈希值. 然后枚举拆点,将它与当前最优的拆点比较谁更优(就是从哪拆的字典序更小),具体方法是二分+哈希找出两串最长的相同 ...
- 大逃杀(树上dp)
这道题和宝藏差不多吧,转移的时候比较麻烦的. 代码中分量很多种情况. h更新比较麻烦 这两幅图表示了双边更新中3,4连个h更新,下面比较好理解的吧. #include<cstring> # ...
- linux 中 stat 函数的用途和使用方法
stat 函数讲解 表头文件: #include <sys/stat.h> #include <unistd.h> 定义函数: int stat(const cha ...