版权声明:本文为博主原创文章,未经博主同意不得转载。

https://blog.csdn.net/u011328934/article/details/26094917

题目连接: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>
#include <cstring>
#include <algorithm> using namespace std;
const int N = 1e5+5; int n, a[N], b[N], p[N], v[N], r[5*N][2]; void init () {
memset(v, 0, sizeof(v)); for (int i = 2; i <= n; i++) {
if (v[i])
continue; for (int j = i * 2; j <= n; j += i)
v[j] = 1;
} for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
p[a[i]] = i;
}
sort(b+1, b+n+1);
} int solve () {
int c = 0; for (int i = 1; i <= n; i++) {
while (p[b[i]] != i) {
for (int j = i; j < p[b[i]]; j++) {
if (!v[p[b[i]] - j + 1]) {
r[c][1] = p[b[i]];
r[c++][0] = j; int t = p[b[i]];
p[b[i]] = j;
p[a[j]] = t;
swap(a[j], a[t]);
break;
}
}
}
}
return c;
} int main () {
scanf("%d", &n);
init();
int c = solve(); printf("%d\n", c);
for (int i = 0; i < c; i++)
printf("%d %d\n", r[i][0], r[i][1]);
return 0;
}

Codefoces 432C Prime Swaps(数论+贪心)的更多相关文章

  1. Codeforces Round #246 (Div. 2) C. Prime Swaps(贪心,数论)

    题目链接:http://codeforces.com/contest/432/problem/C 首先由题意分析出:这些数是从1到n且各不相同,所以最后结果肯定是第i位的数就是i. 采用这样一种贪心策 ...

  2. CodeForces 432C Prime Swaps

    Description You have an array a[1], a[2], ..., a[n], containing distinct integers from 1 to n. Your ...

  3. CodeForces 124C Prime Permutation (数论+贪心)

    题意:给定一个字符串,问你能不能通过重排,使得任意一个素数p <= 字符串长度n,并且 任意的 i <= 长度n/素数p,满足s[p] == s[p*i]. 析:很容易能够看出来,只要是某 ...

  4. Codefoces 432 C. Prime Swaps

    哥德巴赫猜想: 任一大于2的偶数,都可表示成两个素数之和. 任一大于5的整数都可写成三个质数之和. 贪心取尽可能大的素数..... C. Prime Swaps time limit per test ...

  5. Tsinsen A1504. Book(王迪) 数论,贪心

    题目:http://www.tsinsen.com/A1504 A1504. Book(王迪) 时间限制:1.0s   内存限制:256.0MB   Special Judge 总提交次数:359   ...

  6. Codefoces 432 C. Prime Swaps(水)

    思路:从前往后想将1调整好,在调整2....这样平均每次有五次机会调整,并且有相当一部分可能都用不到五次,能够一试.ac 代码: #include<iostream> #include&l ...

  7. 【HDU】2866:Special Prime【数论】

    Special Prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  8. codeforces 680C C. Bear and Prime 100(数论)

    题目链接: C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input s ...

  9. UVA 10140 - Prime Distance(数论)

    10140 - Prime Distance 题目链接 题意:求[l,r]区间内近期和最远的素数对. 思路:素数打表,打到sqrt(Max)就可以,然后利用大的表去筛素数.因为[l, r]最多100W ...

随机推荐

  1. jQuery表格列宽可变,兼容firfox

    本demo使用jQuery包,实现表格列宽可拖拽功能,并实现页面reset时的重新布局.使用jQuery,方便函数的调用,给要处理的表格添加id 后,直接调用$("#id").mo ...

  2. [Tensorflow] Object Detection API - retrain mobileNet

    前言 一.专注话题 重点话题 Retrain mobileNet (transfer learning). Train your own Object Detector. 这部分讲理论,下一篇讲实践. ...

  3. c++ union内存

    看一个例子: #include <iostream> #include <stdio.h> using namespace std; union A { int a; stru ...

  4. LeetCode - 386. Lexicographical Numbers

    Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...

  5. webpack之proxyTable配置

    有两种情况: 一,有统一的项目名 config下index.js 配置如下: proxyTable: { '/tbt_user':{ target: 'http://47.98.216.251:80' ...

  6. day_6.7 py tcp

    2018-6-7 09:20:34 #!/usr/bin/env python #!--*--coding:utf-8 --*-- #!@Time :2018/6/7 9:54 #!@Author T ...

  7. centos 安装教程 服务器配置教程 服务器中安装python 服务器中安装Django 安装MySQL 配置MySQL

    一 .解决python编译安装所需的软件依赖 yum install gcc patch libffi-devel python-devel  zlib-devel bzip2-devel opens ...

  8. ThinkPHP最简教程

    这里不讲原理,只讲操作. 这里不说MVC,只说目录(文件夹)结构. 假设Apache Http Server.PHP.MySql都已经安装完毕并已配置完毕,能够输出phpinfo(). 框架是什么? ...

  9. [No0000134]C#中的委托,匿名方法和Lambda表达式

    简介 在.NET中,委托,匿名方法和Lambda表达式很容易发生混淆.我想下面的代码能证实这点.下面哪一个First会被编译?哪一个会返回我们需要的结果?即Customer.ID=5.答案是6个Fir ...

  10. java工程师学习计划