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 ...
随机推荐
- 《JavaScript设计模式与开发实践》读书笔记之命令模式
1.命令模式 1.1 传统的命令模式 命令模式的应用场景:请求的发送者和请求接收者消除彼此耦合关系 以页面点击按钮为例 点击按钮后,借助命令对象,解开按钮和负责具体行为对象之间的耦合 <body ...
- Learning To Rank之LambdaMART前世今生
1. 前言 我们知道排序在非常多应用场景中属于一个非常核心的模块.最直接的应用就是搜索引擎.当用户提交一个query.搜索引擎会召回非常多文档,然后依据文档与query以及用户的相关程度对 ...
- jQuery的理论基础
概述 jQuery是用JavaScript语言编写的函数库,我们用时,可以直接调用jQuery中相应的函数,对于JavaScript的理解,前面的博客已经介绍过了,在这里只说一下函数的作用,也可以说为 ...
- Qt学习一门:直接使用QT具
今天,通过直接使用QT一些工具来编写命令行程序.你可以看到一种Qt更一般的用法. 内容很easy,输出电流日期. 首先,用一个QDate分类,可以使用QDate类的静态方法currentDate为了得 ...
- AndroidUI的组成部分GridView
java 代码例如以下(简单的知识点我会以凝视的形式解说): package com.gc.gridviewdemo; /** * @author Android将军 */ /** * 知识点解说: ...
- UML之轻松入门(3)-SRP做好厨子,让别人编程去吧
一个厨子能够做出一手好菜,或许他是新东方毕业的或者是祖传秘方.你让他做上一桌佳肴那是简单.快乐而又高效的,然而让他编程就会成为一种苦恼并且让人想不通的一件事.或许这个比喻不是非常恰当,可是对 ...
- Quartz CronTrigger应用
CronTrigger配置格式: 格式: [第二] [支] [小时] [日本] [月] [周] [年] 序号 说明 是否必填 同意填写的值 同意的通配符 1 秒 是 0-59 , ...
- vc++笔记十一
一.LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 连接器LNK是通过调用cvtres.exe完毕文件向coff格式的转换的,所以出现这样的错误的原因就是cvtres.exe出现了问题 ...
- 【DP|多重背包可行性】POJ-1014 Dividing
Dividing Time Limit: 1000MS Memory Limit: 10000K Description Marsha and Bill own a collection of mar ...
- MongoDB:逐渐变得无关紧要
我与MongoDB的关系可分为三个阶段.对于目前处于第三阶段的我来说,这款产品似乎变得无关紧要了.很快你就会明白为什么我这么说. 阶段一:痴迷 我与MongoDB的第一次接触十分神奇:一个poligl ...