codeforces C. Prime Swaps

题意:给你n个数,然后在交换次数小于等于5×n的情况下使得这个序列变成升序,输出次数;

思路:哥德巴赫猜想:任何一个大于5的数都可以写成三个质数之和。尽可能的找大的素数,从1的位置向右逐步的调整,每一个位置最多5次,有的位置不到5次;

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cmath>
 5 #define maxn 100010
 6 using namespace std;
 7
 8 int n;
 9 int a[maxn+10];
10 bool vis[maxn+10];
11 int f[maxn+10];
12 int p[maxn];
13 int s[maxn*5],e[maxn*5];
14 int cnt=0;
15 int bs(int x,int l,int r)
16 {
17     int pos=0;
18     while(l<r)
19     {
20         int mid=(l+r)/2;
21         if(f[mid]>x)
22         {
23             r=mid;
24         }
25         else
26         {
27             l=mid+1;
28             pos=l;
29         }
30     }
31     return pos;
32 }
33 void get_prime()
34 {
35     vis[1]=true;
36     vis[0]=true;
37     for(int i=2; i<maxn; i++)
38     {
39         if(!vis[i])
40         {
41             for(int j=i*2; j<maxn; j+=i)
42             {
43                 vis[j]=true;
44             }
45         }
46     }
47     for(int i=2; i<maxn; i++)
48     {
49         if(!vis[i])
50         {
51            f[cnt++]=i;
52         }
53     }
54 }
55
56 int main()
57 {
58     get_prime();
59     scanf("%d",&n);
60     for(int i=1; i<=n; i++)
61     {
62         scanf("%d",&a[i]);
63         p[a[i]]=i;
64     }
65     int ans=0;
66     for(int i=1; i<=n; i++)
67     {
68         while(a[i]!=i)
69         {
70            int t=bs(p[i]-i+1,0,cnt-1);
71            t--;
72            int sx=p[i],ex=p[i]-f[t]+1;
73            swap(a[sx],a[ex]);
74            swap(p[a[sx]],p[a[ex]]);
75            s[ans]=sx; e[ans++]=ex;
76         }
77     }
78     printf("%d\n",ans);
79     for(int i=0; i<ans; i++)
80     {
81         printf("%d %d\n",e[i],s[i]);
82     }
83     return 0;
84 }

时间: 2024-08-08 22:08:52

codeforces C. Prime Swaps的相关文章

CodeForces 432C Prime Swaps

Description 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?

Codefoces 432C Prime Swaps(数论+贪心)

题目连接:Codefoces 432C Prime Swaps 题目大意:给出一个序列,长度为n,要求用5n以内的交换次数使得序列有序,并且交换的i,j两个位置的数时要满足,j?i+1为素数. 解题思路:a数组为对应的序列,b数组为对应的有序序列,p为对应数的位置.每次从有序序列最小的位置开始,该为必须放b[i]才对,所以p[b[i]]=i,否则就要将b[i]尽量往前换,直到换到i的位置为止. 哥德巴赫猜想:任何一个大于5的数都可以写成三个质数之和. #include <cstdio> #in

Codeforces 912E Prime Gift(预处理 + 双指针 + 二分答案)

题目链接 Prime Gift 题意  给定一个素数集合,求第k小的数,满足这个数的所有质因子集合为给定的集合的子集. 保证答案不超过$10^{18}$ 考虑二分答案. 根据折半的思想,首先我们把这个集合的数分成两组. 然后分别生成这两组质数所能表示出的正整数的集合. 然后把这个集合sort一下,我们得到了两个有序的数列. 在计算小于等于某个数$x$的符合题目条件的数的时候,我们枚举第一个集合中的数, 用双指针定位和当前枚举到的数乘积恰好小于等于$x$的位置. 然后累加. 这里有一个细节,我们要

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 i

codeforces 339E Three Swaps 搜索

题目链接 题意: 给定一个1-n的排列 可以选择一个区间将其翻转.至多翻转三次. 问能不能变成单调递增的序列,并输出方案. 题目保证3次翻转一定有解. 思路: 爆搜,每次翻转一段最长的连续区间. #include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> #include <vector> template <class T> inli

Codeforces 1178D. Prime Graph

传送门 首先每个点至少要有两条边连接 那么容易想到先保证这一点然后再慢慢加边 那么先构成一个环即可:$(1,2),(2,3),(3,4)...(n,1)$ 然后考虑加边,发现一个点加一条边还是合法的,那么不妨直接 $(1,4),(2,5),(3,6)$ ,然后一旦边数为质数了就直接输出答案 那么现在问题就是能否保证在 $[n,n+\left \lfloor \frac {n-3} {2} \right \rfloor]$ 范围内一定有质数 打个表发现在 $n \in [3,1000]$ 内唯一不

Codeforces Round #246 (Div. 2)

A.Choosing Teams 水题 #include <cstdio> #include <cstring> using namespace std; int main() { int n, k, ans, i, x; scanf("%d%d",&n,&k); ans = 0; for(i=0; i<n; ++i) { scanf("%d",&x); if(5-x >= k) ans++; } ans

Codeforces Round #246 (Div. 2) (ABCD详细题解)

比赛链接:http://codeforces.com/contest/432 A. Choosing Teams time limit per test:1 second memory limit per test:256 megabytes The Saratov State University Olympiad Programmers Training Center (SSU OPTC) has n students. For each student you know the numbe

大神刷题表

9月27日 后缀数组:[wikioi3160]最长公共子串 dp:NOIP2001统计单词个数 后缀自动机:[spoj1812]Longest Common Substring II [wikioi3160]最长公共子串 [spoj7258]Lexicographical Substring Search 扫描线+set:[poj2932]Coneology 扫描线+set+树上删边游戏:[FJOI2013]圆形游戏 结论:[bzoj3706][FJ2014集训]反色刷 最小环:[poj1734