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 有序)排序后,整 ...
随机推荐
- Hbuilder快速代码技巧和常用快捷键
1.在body内输入div.abc按下tab键 效果:<divclass="abc"></div> 2.在body内输入div#abc按下tab键 效果:& ...
- [Linux]ubuntu更改国内源
转自: https://blog.csdn.net/qq_35451572/article/details/79516563 推荐快速更新国内源 https://blog.csdn.net/qq_35 ...
- Flume-安装与 NetCat UDP Source 监控端口
Flume 文档:https://flume.apache.org/FlumeUserGuide.html Flume 下载:https://archive.apache.org/dist/flume ...
- Mysql忘记密码,支持中文,tab补全
忘记Mysql密码: (1)暂停服务: /etc/init.d/mysqld stop (2)跳过grant表授权,进入安全模式,并在后台运行: mysqld_safe --skip-grant-ta ...
- Android视频直播全屏实现
/** * 添加直播组件 */ @SuppressLint("JavascriptInterface") private void addPlayerLive(final Subj ...
- JVM菜鸟进阶高手之路一[z]
https://mp.weixin.qq.com/s/qD1LFmsOiqZHD8iZX97OfA? 问题现象 代码如下,使用 ParNew + Serial Old 回收器组合与使用 ParNew ...
- java设计模式中的动态代理
Java的三种代理模式 1.代理模式 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象访问目标对象.这样做的好处是:可以在目标对象实现的基础上,增强额外的功能操作,即扩 ...
- docker 导出多个镜像合并成一个tar
导出单个镜像 docker save [images] > [name.tar] 倒出多个镜像合并成一个tar包 docker save [images] [images] > [name ...
- Windows配置jdk环境变量
配置环境变量 前置条件:已经安装jdk以及jre 操 作:右击"我的电脑"-->"属性"-->"高级系统设置"-->&qu ...
- ASP.NET Core 入门笔记3,使用ASP.NET Core MVC框架构建Web应用
一.ASP.NET Core MVC 输出Hello World,Friend! 1.引入 ASP.NET Core MVC 修改应用启动类(Startup.cs),引入MVC模块并配置默认路由 pu ...