Reordering the Cows

牛客网传送

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format:%lld
链接:https://ac.nowcoder.com/acm/contest/4860/B
来源:牛客网

题目描述

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.

示例1
输入

5
5
1
4
2
3
2
5
3
1
4

输出

2 3

题意:
比如样例:通过移动A,将A=B
A 5 1 4 2 3
B 2 5 3 1 4
id 1 2 3 4 5
首先A中5要到1的位置(id=2),然后1移动到2的位置上(id=4),2要移动到5的位置上上(id=1)就是一个环,5->1->2->5,移动次数为3(也就是环的长度),除此之外还有4->3->4,移动次数为2,问将A通过移动转化成B的过程中,最长移动次数(即最长的环)是多少?环的数量是多少?
题解:
(第一二个代码讲述的是我做错的过程,正解在是第三个)
这题是我难以忘记的痛o(╥﹏╥)o
在这里插入图片描述看完这个题第一反应是noip考过的信息传递,所以一开始就用并查集来做,father[A[id]]=B[id](就是id相对于A的父亲节点是B),因为一定能成环,所以就找一共多少个环,并求出最长环的长度
然而。。。。在这里插入图片描述
我调了一阵子也但还是段错误,我换了个oj测评一下,然后就就AC了。。。玄学(可能这个oj数据弱

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int a[10000];
int b[194000];
int father[10003];
bool f[100004];
int maxx=0;
void unionn(int x,int y)
{father[x]=y;
}
int find(int x,int z)
{if(father[x]==x)return 0;//如果这个数在A中与B中位置相同,不需要交换else if(f[x]==1&&z==0)return 0;//如果这个数曾经被查询过else if(f[x]==1&&z!=0)return 1;//如果这个环第一次被查询z++;//z只有0和非0,来表示这个数之前是否被查询过f[x]=1;maxx=max(maxx,z);find(father[x],z);//不断地向后查找5->1->2
}
int main()
{cin>>n;for(int i=1;i<=n;i++){cin>>a[i];}for(int j=1;j<=n;j++){cin>>b[j];unionn(a[j],b[j]);}int tot=0;for(int i=1;i<=n;i++){if(a[i]!=b[i])tot++;}if(tot==0){cout<<0<<" "<<-1;return 0;}int ant=0;for(int i=1;i<=n;i++){if(find(i,0))ant++;}cout<<ant<<" "<<maxx;
}

我痛定思痛,感觉是因为自身递归导致段错误,又改了一个思路,在寻找根节点并压缩路径的时候,顺便用dis来记录路径的长度,然后统计出最长的路径。
结果没段错误,但。。。wa了。。唉

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int a[10030];
int b[19333];
int father[100333];
bool f[10034];//标记这个点所在的环是否被查询过
int dis[100022];
int maxx=0;
//void unionn(int x,int y)
//{
//  father[x]=y;
//}
void init()
{for(int i=1;i<=n;i++){father[i]=i;}
}
int find(int x)
{if(father[x]!=x){int last=father[x];father[x]=find(father[x]);dis[x]+=dis[last];}return father[x];
}
void unionn(int aa,int bb)
{if(aa==bb)return;int x=find(aa);int y=find(bb);if(x!=y){father[x]=y;dis[aa]=dis[bb]+1;}else maxx=max(maxx,dis[aa]+dis[bb]+1);//更新最长边
}
//int find(int x,int z)
//{
//  if(father[x]==x)return 0;
//  else if(f[x]==1&&z==0)return 0;
//  else if(f[x]==1&&z!=0)return 1;
//  z++;
//  f[x]=1;
//  maxx=max(maxx,z);
//  find(father[x],z);
//}
int main()
{cin>>n;for(int i=1;i<=n;i++){cin>>a[i];}for(int j=1;j<=n;j++){cin>>b[j];}init();int tot=0;for(int i=1;i<=n;i++){if(a[i]!=b[i])tot++;else f[a[i]]=1;}if(tot==0){cout<<0<<" "<<-1;return 0;}int ant=0;//多少个环for(int i=1;i<=n;i++){unionn(a[i],b[i]);}for(int i=1;i<=n;i++){if(f[father[i]]==0){ant++;f[father[i]]=1;}}cout<<ant<<" "<<maxx;
}

我冷静一会后,重新思考,感觉把题越想越复杂了
样例:
A 5 1 4 2 3
B 2 5 3 1 4
f[]来标记id是否被查询过
我们用fa和fb来记录A和B中数的id位置,然后在union时,通过father[]来让 A中的点对应B的位置相互指向,A中id=1的5指向id=2的1,father[5]=1,依次类推
A ------->5 1 4 2 3
fa------->2 4 5 3 1
B-------->2 5 3 1 4
fb-------->4 1 3 5 2
father—>2 4 5 1 3
在查找时for(i->n),查询该点i后,接着查与这点i相连的点father[i],并用f[]标记,ans记录长度,如果相连的点>2(也就是这个点不是指向自己),统计最长环长,并记录换的数量

#include<bits/stdc++.h>
#define for(n) for(int i=1;i<=n;i++)
using namespace std;
typedef long long ll;
int n;
int a[10030];
int b[19333];
int father[100333];
int fa[10034];
int fb[10034];
int f[100022];
int maxx=0;
int ans;
int id;
int tot=0;
int w=0;
int find()
{for(n){if(f[i]==0){id=i;ans=0;while(f[id]==0){f[id]=1;ans++;id=father[id];}if(ans>1){tot=max(ans,tot);w++;}}}return tot;
}
void init()
{for(n){fa[a[i]]=i;	fb[b[i]]=i;}
}
void init1()
{for(n){father[fa[a[i]]]=fb[a[i]];}	}
//void unionn(int x,int y)
//{
//	father[x]=y;
//}
//void unionn(int aa,int bb)
//{
//	if(aa==bb)return;
//	int x=find(aa);
//	int y=find(bb);
//	if(x!=y)
//	{
//		father[x]=y;
//		dis[aa]=dis[bb]+1;
//	}
//	else maxx=max(maxx,dis[aa]+dis[bb]+1);
//}
//	for(int i=1;i<=n;i++)
//	{
//		if(a[i]!=b[i])tot++;
//		else f[a[i]]=1;
//	}
//	if(tot==0)
//	{
//		cout<<0<<" "<<-1;
//		return 0;
//	}
//	int ant=0;
//	for(int i=1;i<=n;i++)
//	{
//		
//		unionn(a[i],b[i]);
//	}
//	for(int i=1;i<=n;i++)
//	{
//		if(f[father[i]]==0)
//		{
//			ant++;
//			f[father[i]]=1;
//		}
//	}
//	cout<<ant<<" "<<maxx;
int main()
{cin>>n;for(n)cin>>a[i];for(n)cin>>b[i];init();init1();if(find()==0)cout<<0<<" "<<-1;else cout<<w<<" "<<tot;}

终于做出来了。。。我太菜了o(╥﹏╥)o

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/320500.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Educational Codeforces Round 54 (Rated for Div.2)

Educational Codeforces Round 54 (Rated for Div.2) D. Edge Deletion 题意&#xff1a;一张n个点的无向图&#xff0c;保留其中k条边&#xff0c;使得有尽可能多的点与1的最短路长度不变。 做法&#xff1a;求出最短路树&#xff0c;然后自底向上删边即可。 #include <bit…

回顾4180天在腾讯使用C#的历程,开启新的征途

今天是2018年8月8日&#xff0c;已经和腾讯解除劳动关系&#xff0c;我的公司正式开始运营&#xff0c;虽然还有很多事情需要理清&#xff0c;公司官网也没有做&#xff0c;接下来什么事情都需要自己去完成了&#xff0c;需要一步一个脚印去完善&#xff0c;开启一个新的征途。…

【dfs】【拓扑排序】组合树

组合树 题目大意&#xff1a; 有一棵树&#xff0c;每个点都有自己的原颜色和目标颜色&#xff08;黑或白&#xff09;&#xff0c;现在深度不小于k的点可以让自己祖宗k代k个点的颜色全部取反&#xff0c;现在问当前树是否能变成目标树 输入样例 2 3 2 1 2 2 3 0 0 0 1 0 1…

P5906-[模板]回滚莫队不删除莫队

正题 题目链接:https://www.luogu.com.cn/problem/P5906 题目大意 nnn个数字&#xff0c;mmm个询问[l,r][l,r][l,r]中最远的相同数字对。 解题思路 我们考虑如何用莫队维护&#xff0c;对于一个询问[l,r][l,r][l,r]&#xff0c;我们先按照lll的块排再按照rrr排&#xff0c;定…

Secret Code(原题和变形题)

洛谷传送 牛客网题一 牛客网题二 没错牛客网有两个题&#xff0c;牛客网题一和洛谷是一样的题&#xff0c;牛客网题二是题一的变形 时间限制&#xff1a;C/C 1秒&#xff0c;其他语言2秒 空间限制&#xff1a;C/C 262144K&#xff0c;其他语言524288K 64bit IO Format: %lld 链…

Codeforces Round #520 (Div. 2)

Codeforces Round #520 (Div. 2) D. Fun with Integers 题意&#xff1a;a与b之间有边&#xff0c;当且仅当存在一个\(x\)使得\(a*b x\)或 \(a*x b\)&#xff0c;这条边的边权为\(|x|\)&#xff0c;保证\(|a|,|b|,|x|<n\)&#xff0c;问一条最长的不走重复边的路径的长度是…

稳定工作和创业之间的抉择

早上写的文章《回顾4180天在腾讯使用C#的历程&#xff0c;开启新的征途》是我在腾讯写的最后一篇对过往10年在腾讯使用C#语言的总结&#xff0c;今天收到反馈有人在造谣腾讯开始去.net&#xff0c;我被迫辞职了。这非常的不负责任&#xff0c;我必须写这篇文章来辟谣。要说腾讯…

初二模拟赛总结(2019.8.7)

成绩&#xff1a; rankrankranknamenamenamescorescorescoreT1T1T1T2T2T2T3T3T3T4T4T4111hkyhkyhky180180180100100100808080000000222lyflyflyf170170170100100100707070000000333tjhtjhtjh160160160100100100404040000202020444fyfyfy160160160606060100100100000000555cyzcy…

牛客2020年愚人节比赛

欢乐的一晚上 题目链接 其实做做也挺好&#xff0c;脑筋急转弯&#xff0c;不需要算法不需要数据结构&#xff0c;纯娱乐 还有不知道是哪位哥的&#xff0c;心疼一下 题解 注&#xff1a;一下题解没必要较劲&#xff0c;欢乐局而已 对不对无所谓&#xff0c;换了最重要奥 A题ra…

P4655-[CEOI2017]Building Bridges【斜率优化dp,CDQ分治】

正题 题目链接:https://www.luogu.com.cn/problem/P4655 题目大意 nnn座桥&#xff0c;删除第iii座会产生wiw_iwi​的代价&#xff0c;相邻的两座桥i,ji,ji,j会产生(hi−hj)2(h_i-h_j)^2(hi​−hj​)2的代价&#xff0c;要求代价最小。 解题思路 设fif_ifi​表示留到第iii座桥…

Codefroces1077F2. Pictures with Kittens (hard version)

Codefroces1077F2. Pictures with Kittens (hard version) 做法&#xff1a;裸的单调队列优化dp #include <bits/stdc.h> #define P pair<ll,ll> #define fr first #define sc second typedef long long ll; using namespace std; int n, m, x; ll dp[5002][5002],…

IdentityServer4 知多少

1. 引言现在的应用开发层出不穷&#xff0c;基于浏览器的网页应用&#xff0c;基于微信的公众号、小程序&#xff0c;基于IOS、Android的App&#xff0c;基于Windows系统的桌面应用和UWP应用等等&#xff0c;这么多种类的应用&#xff0c;就给应用的开发带来的挑战&#xff0c;…

【线段树】矮人排队(jzoj(gz) 3236)

矮人排队 jzoj &#xff08;gz&#xff09;3236 题目大意&#xff1a; 有n个人&#xff0c;高度分别为1,2……n&#xff08;高度按输入来看&#xff09;&#xff0c;现在有两种操作 1&#xff1a;把第x个人和第y个人换一下 2&#xff1a;询问高度为A&#xff0c;A1……B这B-…

牛客网【每日一题】4月2日 月月查华华的手机

牛客网链接 时间限制&#xff1a;C/C 2秒&#xff0c;其他语言4秒 空间限制&#xff1a;C/C 262144K&#xff0c;其他语言524288K 64bit IO Format: %lld 题目描述 月月和华华一起去吃饭了。期间华华有事出去了一会儿&#xff0c;没有带手机。月月出于人类最单纯的好奇心&#…

P3466-[POI2008]KLO-Building blocks【Treap】

正题 题目链接:https://www.luogu.com.cn/problem/P3466 题目大意 nnn个数&#xff0c;每次可以让一个111或−1-1−1&#xff0c;要求操作次数最少使得有连续kkk个相同的。 解题思路 枚举是哪kkk个&#xff0c;然后用平衡树&#xff08;或对顶堆&#xff09;维护中位数和比中…

Codeforces1080F. Katya and Segments Sets

Codeforces1080F. Katya and Segments Sets 题意&#xff1a;给定n个集合&#xff0c;每个集合里有一些区间\([l_i,r_i]\)&#xff0c;有m次询问&#xff0c;每次询问区间\([x,y]\)中&#xff0c;是否包含了集合a到集合b中每个集合至少一个区间。 做法&#xff1a;按区间右端点…

【二分】Best Cow Fences(poj 2018)

Best Cow Fences poj 2018 题目大意&#xff1a; 给出一个正整数数列&#xff0c;要你求平均数最大&#xff0c;长度不小于M的字串&#xff0c;结果乘1000取整 输入样例 10 6 6 4 2 10 3 8 5 9 4 1输出样例 6500数据范围 1⩽N⩽100,0001\leqslant N \leqslant 100,0001⩽…

离散哈特莱变换(DHT)及快速哈特莱变换(FHT)学习

离散哈特莱变换(DHT)及快速哈特莱变换(FHT)学习 说在前边 最近复习\(DSP\)的时候&#xff0c;发现了一个号称专门针对离散实序列的变换&#xff0c;经分析总运算量为普通\(FFT\)的几乎一半&#xff0c;而且完全没有复数。这么强的吗&#xff1f;于是花了一个下午&#xff0c;去…

P2495-[SDOI2011]消耗战【虚树,dp】

正题 题目链接:https://www.luogu.com.cn/problem/P2495 题目大意 nnn个点的一棵树&#xff0c;mmm次给出一些点&#xff0c;要求割掉最小权值的边使得这些点不和111号点联通。 解题思路 根据这些给出的点构造一棵虚树&#xff0c;然后直接dpdpdp求解即可。 codecodecode #i…

【贪心】Sunscreen(poj 3614/luogu 2887)

Sunscreen poj 3614 luogu 2887 题目大意&#xff1a; 有n个人&#xff0c;每个人要求选一个价值在minniminn_iminni​到maxximaxx_imaxxi​的物品&#xff0c;现在有m件物品&#xff0c;每件的价值是spfispf_ispfi​&#xff0c;可以选covericover_icoveri​次&#xff0c…