题意:给出一组数,要求从小到大排序,并且排序的过程中,发生交换的两个数至少一个为幸运数(十进制位均为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)【思维】的更多相关文章

  1. CodeForces 605A Sorting Railway Cars 思维

    早起一水…… 题意看着和蓝桥杯B组的大题第二道貌似一个意思…… 不过还是有亮瞎双眼的超短代码…… 总的意思呢…… 就是最长增长子序列且增长差距为1的的…… 然后n-最大长度…… 这都怎么想的…… 希望 ...

  2. Stack Sorting CodeForces - 911E (思维+单调栈思想)

    Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty) ...

  3. Codeforces 424A (思维题)

    Squats Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Statu ...

  4. Vladik and Complicated Book CodeForces - 811B (思维实现)

    Vladik had started reading a complicated book about algorithms containing n pages. To improve unders ...

  5. Codeforces 1060E(思维+贡献法)

    https://codeforces.com/contest/1060/problem/E 题意 给一颗树,在原始的图中假如两个点连向同一个点,这两个点之间就可以连一条边,定义两点之间的长度为两点之间 ...

  6. Queue CodeForces - 353D (思维dp)

    https://codeforces.com/problemset/problem/353/D 大意:给定字符串, 每一秒, 若F在M的右侧, 则交换M与F, 求多少秒后F全在M左侧 $dp[i]$为 ...

  7. AC日记——Cards Sorting codeforces 830B

    Cards Sorting 思路: 线段树: 代码: #include <cstdio> #include <cstring> #include <iostream> ...

  8. Lucky Array Codeforces - 121E && Bear and Bad Powers of 42 Codeforces - 679E

    http://codeforces.com/contest/121/problem/E 话说这题貌似暴力可A啊... 正解是想出来了,结果重构代码,调了不知道多久才A 错误记录: 1.线段树搞混num ...

  9. ACM-ICPC 2018 徐州赛区现场赛 I. Rikka with Sorting Networks (思维+DFS)

    题目链接:https://codeforces.com/gym/102012/problem/I 题意:问有多少个 1 到 n 的排列,使得用给定的 k 个比较器(使 au 和 av 有序)排序后,整 ...

随机推荐

  1. 阿里云Ubuntu安装LNMP环境之Mysql

    在QQ群很多朋友问阿里云服务器怎么安装LNMP环境,怎么把项目放到服务器上面去,在这里,我就从头开始教大家怎么在阿里云服务器安装LNMP环境. 在这之前,我们先要知道什么是LNMP. L: 表示的是L ...

  2. axios 是如何封装 HTTP 请求的

    原载于 TutorialDocs 网站的文章<How to Implement an HTTP Request Library with Axios>.译者:zhangbao90shttp ...

  3. Nslookup: command not found error on RHEL/CentOS 7

    Reference: https://unix.stackexchange.com/questions/164210/nslookup-command-not-found-error-on-rhel- ...

  4. JDK与CGlib动态代理的实现

    应用的原型为 执行者:房屋中介Agency(分为JDKAgency.CGlibAgency) 被代理对象:程序员Programmer 被代理对象的实现接口:租户Tenement(CGlibAgency ...

  5. Android跨进程通信广播(Broadcast)

    广播是一种被动跨进程通讯的方式.当某个程序向系统发送广播时,其他的应用程序只能被动地接收广播数据.这就象电台进行广播一样,听众只能被动地收听,而不能主动与电台进行沟通,在应用程序中发送广播比较简单.只 ...

  6. C#剪切生成高质量缩放图片

    /// <summary> /// 高质量缩放图片 /// </summary> /// <param name="OriginFilePath"&g ...

  7. JDK1.8为什么废弃永久代【一篇就够】[z]

    https://blog.csdn.net/sjmz30071360/article/details/89456177 (Metaspace) 1.背景 2.为什么废弃永久代(PermGen) 3.深 ...

  8. Python Flask,cookie,session ,设置、获取、删除

    使用Response类的set_cookie()方法可以设置cookie: Response.set_cookie( key, //键 value='', //值 max_age=None, //秒为 ...

  9. MATLAB学习(四)线性方程求解,多项式运算,函数局部最优解

    >> A=[2 1 3;1 3 2];B=[5;10] B = 5 10 >> X=lsqnonneg(A,B) X = 0 2.8571 0.7143 >> A\ ...

  10. dataset的find查找功能使用

    var record = dataset.find(["status"],[curstatus]); //status指的是dataset中的某个字段,curstatus指的是指定 ...