Reordering the Cows

时间限制: 1 Sec  内存限制: 64 MB
提交: 18  解决: 7
[提交][状态][讨论版]

题目描述

Farmer
John's N cows (1 <= N <= 100), conveniently numbered 1..N, are
standing in a row.  Their ordering is described by an array A, where
A(i) is the number of the cow in position i.  Farmer John wants to
rearrange
them into a different ordering for a group
photo, described by an array B, where B(i) is the number of the cow
that should end up in position i.

For example, suppose the cows start out ordered as follows:
A = 5 1 4 2 3
and suppose Farmer John would like them instead to be ordered like this:
B = 2 5 3 1 4

To re-arrange themselves from the "A"
ordering to the "B" ordering, the cows perform a number of "cyclic"
shifts.  Each of these cyclic shifts begins with a cow moving to her
proper location in the "B" ordering, displacing another cow, who then
moves to her proper location, displacing another cow, and so on, until
eventually a cow ends up in the position initially occupied by the first
cow on the cycle.  For example, in the ordering above, if we start a
cycle with cow 5, then cow 5 would move to position 2, displacing cow 1,
who moves to position 4, displacing cow 2, who moves to position 1,
ending the cycle.  The cows keep performing cyclic shifts until every
cow eventually ends up in her proper location in the "B" ordering. 
Observe that each cow participates in exactly one cyclic shift, unless
she occupies the same position in the "A" and "B" orderings.

Please compute the number of different
cyclic shifts, as well as the length of the longest cyclic shift, as the
cows rearrange themselves.

输入

* Line 1: The integer N.
* Lines 2..1+N: Line i+1 contains the integer A(i).
* Lines 2+N..1+2N: Line 1+N+i contains the integer B(i).

输出

*
Line 1: Two space-separated integers, the first giving the number of
cyclic shifts and the second giving the number cows involved in the
longest such shift.  If there are no cyclic shifts,output -1 for the
second number.

样例输入

5
5
1
4
2
3
2
5
3
1
4

样例输出

2 3

提示

There are two cyclic shifts, one involving cows 5, 1, and 2, and the other involving cows 3 and 4.

【分析】这一题我想复杂了,其实循环也可以做,我使用并查集找环的。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
int n,m,k,l,tot=;
int a[N],b[N];
int parent[N],pre[N],vis[N];
int mark[N][N];
vector<int>vec[N],ans[N*N];
int Find(int x)
{
if(parent[x]!=x)parent[x]=Find(parent[x]);
return parent[x];
}
void Union(int x,int y)
{
x=Find(x);
y=Find(y);
if(x==y)return;
else parent[y]=x;
}
void bfs(int s,int t)
{
met(vis,);
met(pre,);
queue<int>q;
q.push(s);
vis[s]=;
while(!q.empty())
{
int u=q.front();
q.pop();
if(u==t)return;
for(int i=; i<vec[u].size(); i++)
{
int v=vec[u][i];
if(!vis[v])
{
pre[v]=u;
vis[v]=;
q.push(v);
}
}
}
}
int main()
{
int u,v;
for(int i=; i<N; i++)parent[i]=i;
scanf("%d",&n);
for(int i=; i<=n; i++)scanf("%d",&a[i]);
for(int i=; i<=n; i++)scanf("%d",&b[i]);
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
if(a[i]==b[j]&&!mark[i][j])
{
mark[i][j]=;
u=i;v=j;
int x=Find(u);
int y=Find(v);
if(x==y)
{
bfs(u,v);
ans[++tot].push_back(v);
while(pre[v])
{
ans[tot].pb(pre[v]);
v=pre[v];
}
}
else
{
vec[u].pb(v);
vec[v].pb(u);
Union(u,v);
}
break;
}
}
}
int aans=,sum=;
for(int i=; i<=tot; i++)
{
if(ans[i].size()>){
sum++;
if(ans[i].size()>aans)aans=ans[i].size();
}
}
if(sum!=)printf("%d %d\n",sum,aans);
else printf("%d -1\n",sum);
return ;
}

(寒假集训)Reordering the Cows的更多相关文章

  1. CSU-ACM寒假集训选拔-入门题

    CSU-ACM寒假集训选拔-入门题 仅选择部分有价值的题 J(2165): 时间旅行 Description 假设 Bobo 位于时间轴(数轴)上 t0 点,他要使用时间机器回到区间 (0, h] 中 ...

  2. (寒假集训)Roadblock(最短路)

    Roadblock 时间限制: 1 Sec  内存限制: 64 MB提交: 9  解决: 5[提交][状态][讨论版] 题目描述 Every morning, FJ wakes up and walk ...

  3. (寒假集训)Cow Art(bfs)

    Cow Art 时间限制: 1 Sec  内存限制: 64 MB提交: 13  解决: 10[提交][状态][讨论版] 题目描述 A little known fact about cows is t ...

  4. (寒假集训)Mooo Moo (完全背包)

    Mooo Moo 时间限制: 1 Sec  内存限制: 64 MB提交: 5  解决: 4[提交][状态][讨论版] 题目描述 Farmer John has completely forgotten ...

  5. (寒假集训) Cow Jog(二分优化的最长上升子数列)

    Cow Jog 时间限制: 1 Sec  内存限制: 64 MB提交: 24  解决: 5[提交][状态][讨论版] 题目描述 Farmer John's N cows (1 <= N < ...

  6. 中南大学2019年ACM寒假集训前期训练题集(基础题)

    先写一部分,持续到更新完. A: 寒衣调 Description 男从戎,女守家.一夜,狼烟四起,男战死沙场.从此一道黄泉,两地离别.最后,女终于在等待中老去逝去.逝去的最后是换尽一生等到的相逢和团圆 ...

  7. 2022寒假集训day2

    day1:学习seach和回溯,初步了解. day2:深度优化搜索 T1 洛谷P157:https://www.luogu.com.cn/problem/P1157 题目描述 排列与组合是常用的数学方 ...

  8. GlitchBot -HZNU寒假集训

    One of our delivery robots is malfunctioning! The job of the robot is simple; it should follow a lis ...

  9. Wooden Sticks -HZNU寒假集训

    Wooden Sticks There is a pile of n wooden sticks. The length and weight of each stick are known in a ...

随机推荐

  1. linux socket c/s上传文件

    这是上传文件的一个示例,可以参照自行修改成下载或者其它功能. 在上传时,需要先将文件名传到服务器端,这是采用一个结构体,包含文件名及文件名长度(可以用于校验),防止文件名乱码. client #inc ...

  2. 基于linux操作系统安装、使用redis详解

    服务端安装 Redis的官方下载站是http://redis.io/download,可以去上面下载最新的安装程序下来,我写此文章时的的稳定版本是2.6.11. 步骤一: 下载Redis 进入软件安装 ...

  3. [洛谷P1903][国家集训队]数颜色

    题目大意:有$n$支画笔,有两个操作 $Q\;l\;r:$询问$[l,r]$中有几种颜色 $R\;p\;Col:$把第$p$支画笔的颜色改成$Col$ 题解:带修莫队,分为$n^{\frac{1}{3 ...

  4. [poj] 3977 Subset || 折半搜索MITM

    原题 给定N个整数组成的数列(N<=35),从中选出一个子集,使得这个子集的所有元素的值的和的绝对值最小,如果有多组数据满足的话,选择子集元素最少的那个. n<=35,所以双向dfs的O( ...

  5. hdu 1512

    思路:用并查集即可,每次合并的时候将小的集合合并到大的集合上去.理论上的平均复杂度是n*lgn*lgn. #include<map> #include<queue> #incl ...

  6. 【POJ 3080 Blue Jeans】

    Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 19026Accepted: 8466 Description The Genogr ...

  7. 当鼠标点击input框时,想让全选input框的所有内容只需要加“onfocus="this.select();”就可以了

    当鼠标点击input框时,想让全选input框的所有内容只需要加“onfocus="this.select();”就可以了

  8. typedef函数用法

    转载自:http://www.cnblogs.com/ggjucheng/archive/2011/12/27/2303238.html 引言 typedef 声明,简称 typedef,为现有类型创 ...

  9. python类中两个列表实例如何相加或相减

    如下 import numpy a = [1, 2, 3, 4] b = [5, 6, 7, 8] a_array = numpy.array(a) b_array = numpy.array(b) ...

  10. 原生js提取非行间样式

    js用style属性可以获得html标签的样式,但是不能获取非行间样式,如何获取css的非行间样式呢,在低版本ie我们可以用currentStyle,在其他浏览器我们可以用getComputedSty ...