Lucky Sorting(CodeForces-109D)【思维】
题意:给出一组数,要求从小到大排序,并且排序的过程中,发生交换的两个数至少一个为幸运数(十进制位均为4或7),问能否在(2×n)次交换内完成排序,如果能,输出交换的方案(不要求步骤数最少)。
思路:首先分为两种情况:
1.所有的数均不为幸运数,则如果给出的序列已经排好序,答案为0,如果未排好序,则无法完成排序。
2.存在幸运数,可得,只要存在幸运数,就一定能在2×n次以内完成排序。
<1>具体的处理方法是将序列分为若干个闭环,闭环的意思可以举个例子来看,(xio表示下标为xi的元素排序后所在位置下标),如:x1->x2->x3->x1,这就是一个闭环,表示x1o==x2,x2o=x3,x3o=x1;
<2>首先,一组数在上述规则下一定能分为若干闭环。
<3>分完闭环后,正式进行交换,并且,确定一个幸运数作为操作数(需要且只需要一个),记录其所在闭环
3.1首先,将操作数所在闭环进行排序(只需相邻之间两两交换即可,具体可参照上文对于闭环的定义)。
3.2再借助操作数对剩余闭环依次排序。第一步为将操作数与待排序闭环中的任意元素交换,再用3.1中的方法操作,最后将操作数归位即可。
3.3很容易证明,上述交换操作的总复杂度一定小于2×n.
代码如下:
#include <cstdio>
#include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
typedef pair<int, int> P;
int n, arr[], inde[];
bool judge(int x)
{
while (x != )
{
if (x % != && x % != )
return false;
x /= ;
}
return true;
} bool cmp(int a, int b)
{
return arr[a] < arr[b];
}
int main()
{
int px = ;
scanf("%d", &n);
for (int i = ; i <= n; i++)
inde[i] = i;
for (int i = ; i <= n; i++)
{
scanf("%d", &arr[i]);
if (!px && judge(arr[i]))
px = i;
}
sort(inde + , inde + + n, cmp);
int iinde[];
for (int i = ; i <= n; i++)
{
iinde[inde[i]] = i;
}
if (!px)
{
int tans = ;
for (int i = ; i <= n; i++)
if (inde[i] != i)
{
tans = -;
break;
}
printf("%d", tans);
}
else
{
vector<P> ans;
int book[] = {};
vector<int> aha[];
int numAha[] = {}, rd = , fgp;
for (int i = ; i <= n; i++)
{
if (book[i])
continue;
rd++;
int p = i;
while (!book[p])
{
book[p] = ;
aha[rd].push_back(p);
if (p == px)
fgp = rd;
p = inde[p];
}
}
//flagRound
memset(book, , sizeof(book));
if (aha[fgp].size() > )
{
int ic = px;
while (!book[inde[ic]])
{
book[ic] = ;
ans.push_back(P(ic, inde[ic]));
ic = inde[ic];
}
}
//restRound
for (int i = ; i <= rd; i++)
{
if (i == fgp || aha[i].size() == )
continue;
int ic = aha[i][];
ans.push_back(P(iinde[px], ic));
while (!book[inde[ic]])
{
book[ic] = ;
ans.push_back(P(ic, inde[ic]));
ic = inde[ic];
}
ans.push_back(P(iinde[px], iinde[aha[i][]]));
}
printf("%d\n", ans.size());
for (int i = ; i < ans.size(); i++)
{
printf("%d %d\n", ans[i].first, ans[i].second);
}
}
return ;
}
By xxmlala
Lucky Sorting(CodeForces-109D)【思维】的更多相关文章
- CodeForces 605A Sorting Railway Cars 思维
早起一水…… 题意看着和蓝桥杯B组的大题第二道貌似一个意思…… 不过还是有亮瞎双眼的超短代码…… 总的意思呢…… 就是最长增长子序列且增长差距为1的的…… 然后n-最大长度…… 这都怎么想的…… 希望 ...
- Stack Sorting CodeForces - 911E (思维+单调栈思想)
Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty) ...
- Codeforces 424A (思维题)
Squats Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Statu ...
- Vladik and Complicated Book CodeForces - 811B (思维实现)
Vladik had started reading a complicated book about algorithms containing n pages. To improve unders ...
- Codeforces 1060E(思维+贡献法)
https://codeforces.com/contest/1060/problem/E 题意 给一颗树,在原始的图中假如两个点连向同一个点,这两个点之间就可以连一条边,定义两点之间的长度为两点之间 ...
- Queue CodeForces - 353D (思维dp)
https://codeforces.com/problemset/problem/353/D 大意:给定字符串, 每一秒, 若F在M的右侧, 则交换M与F, 求多少秒后F全在M左侧 $dp[i]$为 ...
- AC日记——Cards Sorting codeforces 830B
Cards Sorting 思路: 线段树: 代码: #include <cstdio> #include <cstring> #include <iostream> ...
- Lucky Array Codeforces - 121E && Bear and Bad Powers of 42 Codeforces - 679E
http://codeforces.com/contest/121/problem/E 话说这题貌似暴力可A啊... 正解是想出来了,结果重构代码,调了不知道多久才A 错误记录: 1.线段树搞混num ...
- ACM-ICPC 2018 徐州赛区现场赛 I. Rikka with Sorting Networks (思维+DFS)
题目链接:https://codeforces.com/gym/102012/problem/I 题意:问有多少个 1 到 n 的排列,使得用给定的 k 个比较器(使 au 和 av 有序)排序后,整 ...
随机推荐
- OSX 改变PHP安装路径环境变量
当使用XAMPP来学习Laravel的时候,用composer安装laravel总是报错,说mcrypt is required ,但是当我在终端里打印 which php 显示的是usr/bin/p ...
- http协议格式详解
参考:https://www.jianshu.com/p/8fe93a14754c 一.URI结构 HTTP使用统一资源标识符(URI)来传输数据和建立连接.URL(统一资源定位符)是一种特殊种类的U ...
- python Telnet通讯
摘要: python中telnetlib模块的使用: http://blog.csdn.net/five3/article/details/8099997 python实现telnet: http:/ ...
- mysql zip方式安装
下载zip文件解压到安装目录,此时是没有data文件夹和my.ini文件的. 1.首先自己新建my.ini,内容如下: [client] port=3306 default-character-set ...
- HearthBuddy遇奥秘解决方法
https://tieba.baidu.com/g/5808796816 链接: https://pan.baidu.com/s/1NPQTOfxbN_4alP7J-XWuVw 密码: xfj1
- 封装带SSH跳板机的MYSQL
一.封装带SSH跳板机的MYSQL 二.配置settting import pymysql from sshtunnel import SSHTunnelForwarder class MyDb(ob ...
- springboot内置分页技术
1,在pom.xml中注入分页的配置 <dependency> <groupId>com.github.pagehelper</groupId> <artif ...
- 自动化测试 | 好用的自动化测试工具Top 10
欲善其事必先利其器,本文从软件测试人员痛点出发,介绍如何先从工具选择上取得优势,在有限的时间内完成工作.经常有人在公众号留言或是后台咨询,做自动化测试用哪个工具好,或是学哪门编程语言好呢? 这个时候总 ...
- smarty section 循环不同的四个样式
<div class="moban_spzs"> <{section name=goodslist loop=$strdata6}> <{if $sm ...
- 为什么HashMap继承了AbstractMap还要实现Map?
前言 之前看源码一直忽略了这个现象,按理说HashMap的父类AbstractMap已经实现了Map,它为什么还要实现一次呢?遂上网查了一下,背后原因让人大跌眼镜. 原因 这是类库设计者的拼写错误,其 ...