Codefoces 432 C. Prime Swaps
哥德巴赫猜想:
任一大于5的整数都可写成三个质数之和。
贪心取尽可能大的素数.....
2 seconds
256 megabytes
standard input
standard output
You have an array a[1], a[2], ..., a[n], containing distinct integers from 1 to n.
Your task is to sort this array in increasing order with the following operation (you may need to apply it multiple times):
- choose two indexes, i and j (1 ≤ i < j ≤ n; (j - i + 1) is
a prime number); - swap the elements on positions i and j; in other
words, you are allowed to apply the following sequence of assignments: tmp = a[i], a[i] = a[j], a[j] = tmp (tmp is
a temporary variable).
You do not need to minimize the number of used operations. However, you need to make sure that there are at most 5n operations.
The first line contains integer n (1 ≤ n ≤ 105).
The next line contains n distinct integers a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ n).
In the first line, print integer k (0 ≤ k ≤ 5n) —
the number of used operations. Next, print the operations. Each operation must be printed as "i j"
(1 ≤ i < j ≤ n; (j - i + 1) is
a prime).
If there are multiple answers, you can print any of them.
3
3 2 1
1
1 3
2
1 2
0
4
4 2 3 1
3
2 4
1 2
2 4
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int maxn=100100; bool vis[maxn];
int prime[maxn/10],pn;
int n,a[maxn],b[maxn];
int Left[maxn*5],Right[maxn*5],nu; void get_prime()
{
for(int i=2;i*i<maxn;i++)
{
for(int j=i*2;j<maxn;j+=i)
{
vis[j]=1;
}
}
for(int i=2;i<maxn;i++)
{
if(vis[i]==0)
prime[pn++]=i;
}
} int Bin(int x)
{
int low=0,high=pn-1,mid,ans=-1;
while(low<=high)
{
mid=(low+high)/2;
if(prime[mid]<=x)
{
ans=prime[mid],low=mid+1;
}
else high=mid-1;
}
return ans;
} void debug()
{
cout<<"a....\n"; for(int i=1;i<=n;i++) cout<<a[i]<<","; cout<<endl;
cout<<"b....\n"; for(int i=1;i<=n;i++) cout<<b[i]<<","; cout<<endl;
} int main()
{
get_prime();
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",a+i);
b[a[i]]=i;
}
for(int i=1;i<=n;i++)
{
/// from b[i] to i greedy!!!
int len=b[i]-i+1;
while(len!=1)
{
int bin=Bin(len);
int cp=b[i]-bin+1; /// changepos
Left[nu]=cp,Right[nu]=b[i]; nu++;
int t1=b[i],t2=b[a[cp]];
swap(a[cp],a[b[i]]);
b[a[b[i]]]=t1;b[i]=t2;
// debug(); getchar();
len=b[i]-i+1;
}
}
printf("%d\n",nu);
for(int i=0;i<nu;i++)
{
printf("%d %d\n",Left[i],Right[i]);
}
return 0;
}
版权声明:来自: 代码代码猿猿AC路 http://blog.csdn.net/ck_boss
Codefoces 432 C. Prime Swaps的更多相关文章
- Codefoces 432 C. Prime Swaps(水)
思路:从前往后想将1调整好,在调整2....这样平均每次有五次机会调整,并且有相当一部分可能都用不到五次,能够一试.ac 代码: #include<iostream> #include&l ...
- Codefoces 432C Prime Swaps(数论+贪心)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/26094917 题目连接:Codefoces ...
- 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 #246 (Div. 2) C. Prime Swaps(贪心,数论)
题目链接:http://codeforces.com/contest/432/problem/C 首先由题意分析出:这些数是从1到n且各不相同,所以最后结果肯定是第i位的数就是i. 采用这样一种贪心策 ...
- codeforces C. Prime Swaps
题意:给你n个数,然后在交换次数小于等于5×n的情况下使得这个序列变成升序,输出次数; 思路:哥德巴赫猜想:任何一个大于5的数都可以写成三个质数之和.尽可能的找大的素数,从1的位置向右逐步的调整,每一 ...
- Prime Factory
Your task is simple:Find the first two primes above 1 million, whose separate digit sums are also pr ...
- Codeforces #432 Div2 D
#432 Div2 D 题意 给出一些数字,如果这些数字的的 \(gcd\) 不为1则称这些数字 \(good\). 可以有两种操作: 花费 x 删掉一个数 花费 y 将一个数加 1 问使这些数 \( ...
- POJ 3126:Prime Path
Prime Path Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit St ...
- Java 素数 prime numbers-LeetCode 204
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
随机推荐
- 怎样让你的安卓手机瞬间变Firefox os 畅玩firefox os 应用
Firefox os 手机迟迟不能在国内大面积上市.如今能买到的Firefox os手机国内就一款Firefox os ZET OPEN C ,但这款手机配置确实还不如人意.价格方面也不实惠,对于我们 ...
- JQuery日记_5.13 Sizzle选择器(六)选择器的效率
当选择表达式不符合高速匹配(id,tag,class)和原生QSA不可用或返回错误时,将调用select(selector, context, results, seed)方法,此方法迭代DO ...
- C语言static 具体分析
google在最后三页C语言static内容,可找到的资料非常少.无论是长篇大论不知所云的话,在关键位置或跳过,习的人来说參考性不是非常大.所以.我这篇博文博採众家之长,把互联网上的资料整合归类,并亲 ...
- UVa 988 - Many Paths, One Destination
称号:生命是非常多的选择.现在给你一些选择(0~n-1),和其他选项后,分支数每一次选择,选择共求. 分析:dp,图论.假设一个状态也许是选择的数量0一个是,代表死亡,计数的路径数将达到所有死亡可以去 ...
- Android 动画具体解释View动画
为了让用户更舒适的在某些情况下,利用动画是那么非常有必要的.Android在3.0一旦支持两种动画Tween动漫Frame动画.Tween动画支持简单的平移,缩放,旋转,渐变.Frame动画就像Gif ...
- Enum变量值的Discretion
有些时候,某个方法的返回值是个枚举类型,比如描述登录结果: public enum LoginResult { Success, WrongPassword, } 当前段UI获取到登陆方法的返回结果时 ...
- PV操作——生产者和消费者
首先,先来看几个概念: 同步:协作的过程,比如,多人开发合作. 相互排斥:争抢资源的过程.比如苦逼的大学选课: 临界区:进程中对临界资源实施操作的那段程序: 临界资源:一次仅仅能一个进程使用的资源,比 ...
- iOS缓存类的设计
使用执行速度缓存的程序可以大大提高程序,设计一个简单的缓存类并不需要太复杂的逻辑. 只需要一个简单的3接口. 存款对象 以一个对象 删除对象 阅读对象 watermark/2/text/aHR0cDo ...
- 重写ViewPager实施单一交有关切换到这个问题,并没有缓存
我们需要解决这个问题:切换时,可以实现单独的消息到左和右,但我不知道的情况下根据岗位总数.只是不知道ViewPagerAdapter的getCount数量. 因为帖子内容的数据图片和布局比較复杂.所以 ...
- 乐在其中设计模式(C#) - 原型模式(Prototype Pattern)
原文:乐在其中设计模式(C#) - 原型模式(Prototype Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 原型模式(Prototype Pattern) 作者:weba ...