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;
}
时间: 2024-10-28 15:36:06

Codefoces 432 C. Prime Swaps(水)的相关文章

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

POJ 2739 Sum of Consecutive Prime Numbers(水题)

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20560   Accepted: 11243 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

UVa 1644 Prime Gap (水题,暴力)

题意:给定一个数 n,求它后一个素数和前一个素数差. 析:先打表,再二分查找. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring&

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?

hdu 5108 Alexandra and Prime Numbers (水题,暴力)

Problem Description Alexandra has a little brother. He is new to programming. One day he is solving the following problem: Given an positive integer N, judge whether N is prime. The problem above is quite easy, so Alexandra gave him a new task: Given

UVA 10200 Prime Time 水

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1141 题意:判断区间[a,b]的f(i)是否为素数,f(i)=i*i+i+40: 思路:打个表,然后注意精度: #include<bits/stdc++.h> using namespace std; #define ll long long #define esp

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

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

大神刷题表

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