【搜索】【并查集】Codeforces 691D Swaps in Permutation
题目链接:
http://codeforces.com/problemset/problem/691/D
题目大意:
给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交换X Y位置上的数字,求可以得到的最大字典序的数列。
题目思路:
【搜索】【并查集】
这题可以用搜索或者并查集写,都能过。
把位置分成若干块,每一块里面的位置都是可以被这一块里另一个位置经过若干次调换的(类似强连通,位置可达)。
然后把每一块位置里的 位置按从小到大排序,位置上的值按从大到小排序,依次填入位置(最大的放最前)。
每个点都会被放且仅放一次。
并查集:
//
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 1000004
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
int fa[N],next[N],last[N],c[N],a[N],b[N],d[N];
bool mark[N];
int zhao(int aa)
{
if(fa[aa]== || fa[aa]==aa)return aa;
return fa[aa]=zhao(fa[aa]);
}
bool cmp1(int aa,int bb)
{
return aa>bb;
}
bool cmp2(int aa,int bb)
{
return aa<bb;
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
int x,y,fx,fy;
// for(scanf("%d",&cass);cass;cass--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s+1))
while(~scanf("%d",&n))
{
mem(last,);mem(mark,);mem(fa,);
scanf("%d",&m);
for(i=;i<=n;i++)
scanf("%d",c+i);
for(i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
fx=zhao(x);
fy=zhao(y);
fa[fy]=fx;
}
for(i=;i<=n;i++)
{
fa[i]=zhao(i);
next[i]=last[fa[i]],last[fa[i]]=i;
}
for(i=;i<=n;i++)
{
if(mark[c[i]])continue;
for(k=,j=last[fa[i]];j;j=next[j])
{
a[++k]=c[j];
b[k]=j;
}
sort(a+,a++k,cmp1);
sort(b+,b++k,cmp2);
for(j=;j<=k;j++)
d[b[j]]=a[j],mark[a[j]]=;
}
for(i=;i<=n;i++)
printf("%d ",d[i]);
puts("");
}
return ;
}
/*
// //
*/
搜索(学长写的):
#include<stdio.h>
#include<algorithm>
#define MAXN 1000002
using namespace std; int a[MAXN],first[MAXN],to[*MAXN],next[*MAXN];
int stack_a[MAXN],stack_loc[MAXN],ans[MAXN];
bool vis[MAXN];
int top; void dfs(int u)
{
int e,v; vis[u]=true;
stack_a[top]=a[u];
stack_loc[top++]=u;
e=first[u];
while(e)
{
v=to[e];
if(!vis[v])dfs(v);
e=next[e];
}
} int main()
{
int n,m,i,j,u,v; scanf("%d%d",&n,&m);
for(i=;i<=n;i++)scanf("%d",&a[i]);
for(i=;i<=m;i++)
{
scanf("%d%d",&u,&v);
next[i]=first[u];
next[i+m]=first[v];
first[u]=i;
first[v]=i+m;
to[i]=v;
to[i+m]=u;
}
for(i=;i<=n;i++)
{
if(!vis[i])
{
top=;
dfs(i);
sort(stack_a,stack_a+top);
sort(stack_loc,stack_loc+top);
for(j=;j<top;j++)ans[stack_loc[j]]=stack_a[top-j-];
}
}
for(i=;i<=n;i++)
{
printf("%d",ans[i]);
printf("%c",i<n?' ':'\n');
} return ;
}
【搜索】【并查集】Codeforces 691D Swaps in Permutation的更多相关文章
- Codeforces 691D Swaps in Permutation
Time Limit:5000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Status Prac ...
- codeforces 691D Swaps in Permutation DFS
这个题刚开始我以为是每个交换只能用一次,然后一共m次操作 结果这个题的意思是操作数目不限,每个交换也可以无限次 所以可以交换的两个位置连边,只要两个位置连通,就可以呼唤 然后连通块内排序就好了 #in ...
- 贪心+bfs 或者 并查集 Codeforces Round #268 (Div. 2) D
http://codeforces.com/contest/469/problem/D 题目大意: 给你一个长度为n数组,给你两个集合A.B,再给你两个数字a和b.A集合中的每一个数字x都也能在a集合 ...
- DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...
- 01背包dp+并查集 Codeforces Round #383 (Div. 2)
http://codeforces.com/contest/742/problem/D 题目大意:有n个人,每个人有重量wi和魅力值bi.然后又有m对朋友关系,朋友关系是传递的,如果a和b是朋友,b和 ...
- CodeForces 691D:Swaps in Permutation(并查集)
http://codeforces.com/contest/691/problem/D D. Swaps in Permutation You are given a permutation of ...
- Educational Codeforces Round 14 D. Swaps in Permutation 并查集
D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...
- Educational Codeforces Round 14 D. Swaps in Permutation (并查集orDFS)
题目链接:http://codeforces.com/problemset/problem/691/D 给你n个数,各不相同,范围是1到n.然后是m行数a和b,表示下标为a的数和下标为b的数可以交换无 ...
- Educational Codeforces Round 14 D. Swaps in Permutation(并查集)
题目链接:http://codeforces.com/contest/691/problem/D 题意: 题目给出一段序列,和m条关系,你可以无限次互相交换这m条关系 ,问这条序列字典序最大可以为多少 ...
随机推荐
- hadoop集群环境搭建之zookeeper集群的安装部署
关于hadoop集群搭建有一些准备工作要做,具体请参照hadoop集群环境搭建准备工作 (我成功的按照这个步骤部署成功了,经实际验证,该方法可行) 一.安装zookeeper 1 将zookeeper ...
- Java-Android 之输入提示框
Android的文本提示框有两种方式: main.xml文件 <?xml version="1.0" encoding="utf-8"?> < ...
- 网站出现 HTTP 错误 401.2 - 未经授权:访问由于服务器配置被拒绝
原因:关闭了匿名身份验证 解决方案: 在开始菜单中输入运行->inetmgr,打开站点属性->目录安全性->身份验证和访问控制->选中"启用匿名访问",输入 ...
- Android之提交数据到服务端方法简单封装
在Android应用中,除了单机版的应用,其余的应用免不了需要频繁地与服务端进行数据交互,如果每一种方法都独立写一段代码,那会造成代码大量重复,冗余,这不是我们所希望的,所以我们可以对其进行一些封装, ...
- c# 为什么要用 get set 属性
1 可以对赋值 做验证 ,范伟限制,额外的限制 2 可以设置 只读 只写 3 可以做线程同步 4 可以将属性设置在interface接口中 5 可以使用虚属性 或 抽象属性 可以填补 没有 虚字段 抽 ...
- 佳博GprinterApp编辑软件使用说明
佳博打印机代理商淘宝店https://shop107172033.taobao.com/index.htm?spm=2013.1.w5002-9520741823.2.Sqz8Pf 在此店购买的打印机 ...
- webServices 执行流程,(我是菜鸟,我怕谁,仅代表个人理解,欢迎各位大神们指导,不和您的胃口,请默默离开!!)
二.上图仅仅代表个人理解,下面以代码方式解释一下. (1) strtus.xml <?xml version="1.0" encoding="UTF-8" ...
- C# XML文件操作类XmlHelper
类的完整代码: using System;using System.Collections;using System.Xml; namespace Keleyi.Com.XmlDAL{public c ...
- innerHTML/outerHTML; innerText/outerText; textContent
innerHTML v.s. outerHTML Element.innerHTML Reference: https://developer.mozilla.org/en-US/docs/Web/A ...
- 网络编程(学习整理)---2--(Udp)实现简单的控制台聊天室
1.UDP协议: 总结一下,今天学习的一点知识点! UDP也是一种通信协议,常被用来与TCP协议作比较!我们知道,在发送数据包的时候使用TCP协议比UDP协议安全,那么到底安全在哪里呢?怎么理解呢! ...