SPOJ1421_Goods_循环节
题意:1~n的一个排列,两两互换,每个位置每天只能做一次交换,问最多几天能交换成1~n,并且输出交换步骤。
解法:把该置换中所有的循环节找出,各循环节之间的交换是并行的,两两不相关,每天只需在循环节内部交换。
若循环节长度为1,则无需交换,若循环节长度为2,则只需交换一次,若循环长度>2,则只需要交换2次。
置换方法:每个循环节中的第一个和最后一个交换,第二个和倒数第二个交换...
至于为什么只需两次,可以在纸上模拟一下置换过程,按上述置换方法一次交换后,一个循环节变成了(n-1)/2个长度为2的循环节,可以一次并行交换。
代码如下:
/*************************************************************************
> File Name: D.cpp
> Author: Chierush
> Mail: qinxiaojie1@gmail.com
> Created Time: 2013年07月24日 星期三 09时04分46秒
************************************************************************/ #include <iostream>
#include <cstring>
#include <cstdlib>
#include <set>
#include <cstdio>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm> #define LL long long
#define LLU unsigned long long using namespace std; bool vis[5005];
int c[5005],n,a[5005],b[5005],_count;
vector<int>s[5005]; inline void swap(int &x,int &y)
{
x=x^y,y=x^y,x=x^y;
} void dfs(int x)
{
int y=a[x],Count=1;
while (y!=x)
{
++Count;
y=a[y];
}
c[x]=Count;
y=a[x];
while (y!=x)
{
vis[y]=true;
c[y]=Count;
y=a[y];
}
s[_count].clear();
if (Count>1)
{
s[_count].push_back(x);
y=a[x];
while (y!=x)
{
s[_count].push_back(y);
y=a[y];
}
++_count;
}
} int main()
{
scanf("%d",&n);
_count=0;
for (int i=1;i<=n;++i)
scanf("%d",&b[i]);
for (int i=1;i<=n;++i)
a[b[i]]=i;
for (int i=1;i<=n;++i)
if (!vis[i])
{
vis[i]=true;
dfs(i);
}
int ans=0;
for (int i=1;i<=n;++i)
if (c[i]>ans)
ans=c[i];
if (ans==1) ans=0;
else if (ans==2) ans=1;
else ans=2;
printf("%d\n",ans);
while (ans)
{
int z=0;
for (int i=0;i<_count;++i)
z+=s[i].size()/2;
printf("%d",z);
for (int i=0;i<_count;++i)
{
for (int j=0;j<s[i].size()/2;++j)
{
printf(" %d-%d",s[i][j],s[i][s[i].size()-j-1]);
swap(a[s[i][j]],a[s[i][s[i].size()-j-1]]);
}
}
printf("\n");
_count=0;
memset(vis,0,sizeof(vis[0])*(n+1));
memset(c,0,sizeof(c[0])*(n+1));
for (int i=1;i<=n;++i)
if (!vis[i])
{
vis[i]=true;
dfs(i);
}
ans=0;
for (int i=1;i<=n;++i)
if (c[i]>ans)
ans=c[i];
if (ans==1) ans=0;
else if (ans==2) ans=1;
else ans=2;
/*for (int i=1;i<=n;++i)
printf("%d ",a[i]);
printf("\n");*/
}
return 0;
}
SPOJ1421_Goods_循环节的更多相关文章
- UVA 10692 Huge Mods(指数循环节)
指数循环节,由于a ^x = a ^(x % m + phi(m)) (mod m)仅在x >= phi(m)时成立,故应注意要判断 //by:Gavin http://www.cnblogs. ...
- 【POJ 2406】Power Strings(KMP循环节)
终于靠着理解写出KMP了,两种KMP要代码中这种才能求循环节.i-next[i]就是循环节. #include<cstdio> #define N 1000005 char s[N]; i ...
- HNU 12869 Sequence(循环节)
题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12869 解题报告:看到n的范围这么大,一看就是找规律,把前30 ...
- Codeforces Round #383 (Div. 2) A,B,C,D 循环节,标记,暴力,并查集+分组背包
A. Arpa’s hard exam and Mehrdad’s naive cheat time limit per test 1 second memory limit per test 256 ...
- 51Node 1035----最长的循环节
51Node 1035----最长的循环节 正整数k的倒数1/k,写为10进制的小数如果为无限循环小数,则存在一个循环节,求<=n的数中,倒数循环节长度最长的那个数. 1/6= 0.1 ...
- POJ 2185 Milking Grid KMP(矩阵循环节)
Milking Grid Time Limit: 3000MS Memory Lim ...
- KMP模板,最小循环节
(可以转载,但请注明出处!) 下面是有关学习KMP的参考网站 http://blog.csdn.net/yaochunnian/article/details/7059486 http://blog. ...
- HDU 3746 Cyclic Nacklace 环形项链(KMP,循环节)
题意: 给一个字符串,问:要补多少个字符才能让其出现循环?出现循环是指循环节与字符串长度不相等.比如abc要补多个变成abcabc.若已经循环,输出0. 思路: 根据最小循环节的公式,当len%(le ...
- UVA 10298 Power Strings 字符串的幂(KMP,最小循环节)
题意: 定义a为一个字符串,a*a表示两个字符相连,即 an+1=a*an ,也就是出现循环了.给定一个字符串,若将其表示成an,问n最大为多少? 思路: 如果完全不循环,顶多就是类似于abc1这样, ...
随机推荐
- jeesuite分布式框架环境搭建
一.简述 这是菜鸟走向开源的第一步.开源项目jeesuite:http://git.oschina.net/vakinge/jeesuite-libs jeesuite是托管在码云上的开源项目,是一个 ...
- DOM渲染
浏览器通常要求DOM 实现和JavaScript 实现保持相互独立.例如,在Internet Explorer 中,被称为JScript的JavaScript 实现位于库文件jscript.dll 中 ...
- Android-apktool反汇编异常-Input file (XXX) was not found or was not readable.
问题描述: 原因: 在apptool 2.0以后命令发生了变化,不能再使用 apktool d <apk_path> <outfile_path> 解决方法:解决方法: 使用命 ...
- Android开发之 shape的使用
android shape的使用 shape用于设定形状,能够在selector,layout等里面使用,有6个子标签,各属性例如以下: <?xml version="1.0" ...
- WPF UserControl 的绑定事件、属性、附加属性
原文:WPF UserControl 的绑定事件.属性.附加属性 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Vblegend_2013/arti ...
- Android中WebView的相关使用
近期做的项目中,遇到个非常棘手的问题: 客户给我的数据是有限制的,因此,在返回某条详细页面内容的时候,他仅仅能给我一个html片段,里面包括 文字,图片以及附件的下载地址.假设网页模版规范的爱比較好说 ...
- go语言刷leetcode - 14 Longest Common Prefix
func longestCommonPrefix(strs []string) string { { return "" } { ] } ; ; idx++ { ; i < ...
- xshell登陆Win10 Linux子系统
原文:xshell登陆Win10 Linux子系统 版权声明:转载请注明出处 https://blog.csdn.net/anychenp/article/details/78922320 修改端口 ...
- Single-stack real-time operating system for embedded systems
A real time operating system (RTOS) for embedded controllers having limited memory includes a contin ...
- HDU 3153 Pencils from the 19th Century(数学)
主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3153 Problem Description Before "automaton" ...