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 有序)排序后,整 ...
随机推荐
- Spring Boot中@OneToMany与@ManyToOne几个需要注意的问题
@OneToMany如果不加@JoinColumn,系统会自动在主从表中增加一个中间表. 主表: @Entity(name = "Post") public class Post ...
- XGBoost的优点
1. Gradient boosting(GB) Gradient boosting的思想是迭代生多个(M个)弱的模型,然后将每个弱模型的预测结果相加,后面的模型Fm+1(x)基于前面学习模型的Fm( ...
- tomcat使用实践三种运行模式之apr
tomcat有三种运行模式 nio,bio,apr bio是阻塞式IO操作,使用的是传统的java i/o处理方式,对于每一个请求都要创建一个线程来进行处理,所以开销较大不适合处理高并发的场景 nio ...
- Flutter制作Toast会自己关闭的消息提示框
项目中需要用到类似安卓的Toast提示框,因为flutter中又没有相关组件,然后在网上看到个不错的,地址https://www.jianshu.com/p/cf7877c9bdeb,然后拿过来修改了 ...
- 百度地图API--百度地图底色选择
可选择底色列表<select id="stylelist" onchange="changeMapStyle(this.value)"> <o ...
- 黑马lavarel教程---9、缓存操作
黑马lavarel教程---9.缓存操作 一.总结 一句话总结: legend2项目中自己写的哪些文件操作都可以通过这里的缓存实现,简单方便 1.lavarel中如何使用后端主流的缓存如 Memcac ...
- 解决:安装jenkins时web界面出现jenkins实例似乎已离线问题
https://blog.51cto.com/8593714/2318144?tdsourcetag=s_pctim_aiomsg Windows下环境也可以解决:
- centos7 下设置 mongodb 开机启动 (重点)
centos 7的开机启动跟之前版本的centos有很大不同.现在用 systemctl命令代替了之前的chkconfig 和 service 命令 注册到开机启动的方法如下: 在系统服务目录下新建m ...
- rally测试opentack------安装部署和简单实践
1,下载 git clone git://git.openstack.org/openstack/rally 或者 git clone https://git.openstack.org/openst ...
- openstack部署neutron
controller 1.创建数据库并设置权限 mysql -u root -p0330 CREATE DATABASE neutron; GRANT ALL PRIVILEGES ON neutro ...