Codeforces 432C
题意:
给出一个长度为n(n<=10^5)的数组a, 数组a是数字1到n的一种排列(打乱顺序).
每次可以选择两个数(不同)进行交换, 但是交换的条件是被选择的两个数的下标之差加1应该是一个素数.
思路:
比赛的时候WA到爆..好弱, 思路基本都是对的, 贪心. 用数组pos[i]来维护值i在数组中的位置.
对于第i个数, 如果 pos[i]-i+1是素数,那么就直接交换.否则, 需要多次交换来使得a[i] = i;
采用贪心策略, 从i+1开始枚举是否可以与pos[i]进行交换, 这里交换的次数不会很多,
因为只有当m!+2, m!+3, ......., m!+m时会存在m-1个连续的合数, 而n<=10^5, 所以m <=8
所以交换的次数不会超过5次.
附上代码:
/*************************************************************************
> File Name: C.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年05月15日 星期四 23时51分15秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <vector>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; bool is_prime[];
int a[], pos[]; // hold the index of the value of i
typedef pair<int, int> pii;
vector<pii> ans; // keep each move... void
init() {
memset(is_prime, true, sizeof(is_prime));
is_prime[] = false;
for (int i = ; i < ; i++) {
if (is_prime[i]) {
for (int j = i*i; j < ; j += i) {
is_prime[j] = false;
}
}
}
return ;
} int
main(void) {
init();
int n;
scanf("%d", &n);
for (int i = ; i <= n; i++) {
scanf("%d", a + i);
pos[a[i]] = i;
}
for (int i = ; i <= n; i++) {
if (i == a[i]) continue;
if (is_prime[pos[i]-i+]) {
swap(a[i], a[pos[i]]);
ans.push_back(pii(pos[i], i));
pos[a[pos[i]]] = pos[i];
pos[i] = i;
} else {
// distance from index i to pos[i]
int d = pos[i] - i;
// in other words, a[i] will be equal to i when d equals 0
while (d) {
for (int k = d; k >= ; k--) {
if (is_prime[k+]) {
// from index of i+d-k to index of pos[i]
swap(a[i+d-k], a[pos[i]]);
// push to ans
ans.push_back(pii(i+d-k, pos[i]));
// update pos[]
pos[a[pos[i]]] = pos[i];
pos[i] = i+d-k;
// update d
d -= k;
break;
}
}
}
}
}
// for (int i = 1; i <= 10; i++) {
// cout << a[i] << ' ';
// }
// cout << endl;
int len = ans.size();
printf("%d\n", len);
for (int i = ; i < len; i++) {
if (ans[i].first > ans[i].second) {
swap(ans[i].first, ans[i].second);
}
printf("%d %d\n", ans[i].first, ans[i].second);
} return ;
}
Codeforces 432C的更多相关文章
- CodeForces 432C Prime Swaps
Description You have an array a[1], a[2], ..., a[n], containing distinct integers from 1 to n. Your ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- opencv4 java投影
工程下载 https://download.csdn.net/download/qq_16596909/11505994 比较适合与验证码的处理,毕竟八邻域降噪不能消除比较大的噪点,为了尽量减少噪点对 ...
- Leetcode563.Binary Tree Tilt二叉树的坡度
给定一个二叉树,计算整个树的坡度. 一个树的节点的坡度定义即为,该节点左子树的结点之和和右子树结点之和的差的绝对值.空结点的的坡度是0. 整个树的坡度就是其所有节点的坡度之和. 示例: 输入: 1 / ...
- Go之路一
一.声明变量 var a int var b string var c []float32 var d func() bool var e struct{ x int } 第1行,声明一个整型类型的变 ...
- PHP jpgraph的一点小提示和方法
PHP默认是不启用GD库的,因为需要在php.ini的配置文件中将extension=php_gd2.dll注释打开.打开后你就可以画一些你想画的各种奇葩图案了.什么?不会画?那回去学基础! 今天看了 ...
- UI2Code智能生成Flutter代码--整体设计篇
摘要: UI2CODE项目是闲鱼技术团队研发的一款通过机器视觉理解+AI人工智能将UI视觉图片转化为端侧代码的工具. 背景: 随着移动互联网时代的到来,人类的科学技术突飞猛进.然而软件工程师们依旧需要 ...
- HDU6200 mustedge mustedge mustedge
不用看题就知道这是和什么tarjan.缩点或桥一类有关的题. 谁让他取题目叫一个mustedge还连续写3次的(哦,似乎是因为那个比赛的题目都是这个画风) 必须的边 >必须要经过的边 > ...
- 话说placeholder
placeholder 属性提供一种提示(hint),描述输入域所期待的值. 注释:placeholder 属性适用于以下类型的 <input> 标签:text, search, url, ...
- 2019.7.29 NOIP模拟测试10 反思总结【T2补全】
这次意外考得不错…但是并没有太多厉害的地方,因为我只是打满了暴力[还没去推T3] 第一题折腾了一个小时,看了看时间先去写第二题了.第二题尝试了半天还是只写了三十分的暴力,然后看到第三题是期望,本能排斥 ...
- Mysql的CMD操作
一.MySQL登录和退出——在CMD模式操作 l 语法格式:mysql.exe –h主机名 –u用户名 –p密码 l 参数说明: mysql.exe是mysql服务器的主应用程序. -h代 ...
- MS17-010远程溢出漏洞 - 永恒之蓝 [CVE-2017-0143]
MS17-010远程溢出漏洞(永恒之蓝) Ti:2019-12-25 By:Mirror王宇阳 MS17-010 CVE-2017-0143 MS17-010 CVE-2017-0144 MS17-0 ...