题目链接:

  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的更多相关文章

  1. Codeforces 691D Swaps in Permutation

    Time Limit:5000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Prac ...

  2. codeforces 691D Swaps in Permutation DFS

    这个题刚开始我以为是每个交换只能用一次,然后一共m次操作 结果这个题的意思是操作数目不限,每个交换也可以无限次 所以可以交换的两个位置连边,只要两个位置连通,就可以呼唤 然后连通块内排序就好了 #in ...

  3. 贪心+bfs 或者 并查集 Codeforces Round #268 (Div. 2) D

    http://codeforces.com/contest/469/problem/D 题目大意: 给你一个长度为n数组,给你两个集合A.B,再给你两个数字a和b.A集合中的每一个数字x都也能在a集合 ...

  4. DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph

    题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...

  5. 01背包dp+并查集 Codeforces Round #383 (Div. 2)

    http://codeforces.com/contest/742/problem/D 题目大意:有n个人,每个人有重量wi和魅力值bi.然后又有m对朋友关系,朋友关系是传递的,如果a和b是朋友,b和 ...

  6. CodeForces 691D:Swaps in Permutation(并查集)

    http://codeforces.com/contest/691/problem/D D. Swaps in Permutation   You are given a permutation of ...

  7. Educational Codeforces Round 14 D. Swaps in Permutation 并查集

    D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...

  8. Educational Codeforces Round 14 D. Swaps in Permutation (并查集orDFS)

    题目链接:http://codeforces.com/problemset/problem/691/D 给你n个数,各不相同,范围是1到n.然后是m行数a和b,表示下标为a的数和下标为b的数可以交换无 ...

  9. Educational Codeforces Round 14 D. Swaps in Permutation(并查集)

    题目链接:http://codeforces.com/contest/691/problem/D 题意: 题目给出一段序列,和m条关系,你可以无限次互相交换这m条关系 ,问这条序列字典序最大可以为多少 ...

随机推荐

  1. CTE在Oracle和Sqlserver中使用的差异

    CTE是一个很好用的工具,他可以帮助我们清晰代码结构,减少临时表使用,同时oracle和sqlserver都提供支持.但在oracle和sqlserver中使用CTE也存在一定区别. Oracle使用 ...

  2. 重新看php数组

    闲来有空,最近看php手册数组这块,对于array_values() 还是第一次接触,array_values是不保留键名,只有键值的函数,还有一个作用就是  重新索引. unset() 函数,是删除 ...

  3. asp.net利用ajax和jquery-ui实现进度条

    前台用ajax不停进行查询,直到任务完成.进度条用的是jquery-ui.后台用一般处理程序处理相应,进度信息保存在HttpContext.Application中. 代码作为简单示例,实际应用时应对 ...

  4. java simple check whether a file or directory.

    Ref:  check whether a file or directory First, make sure the path exists by using: new File(path).ex ...

  5. Oracle dblink 使用详解

    1.dblink简介 dblink(Database Link)数据库链接就是数据库的链接,跨本地数据库 2.使用语法详解 基本语法 CREATE [SHARED][PUBLIC] database ...

  6. C# 多个线程一直跑着While(true)

    在使用多线程的时候,开了多个线程一直在While(true),会造成CPU占用很高.这时候要在线程内加入一句Thread.Sleep(1),让他稍微睡一下.就不会消耗那么多CPU了. 代码: Thre ...

  7. 基于CDH5.x 下面使用eclipse 操作hive 。使用java通过jdbc连接HIVESERVICE 创建表

    基于CDH5.x 下面使用eclipse 操作hive .使用java通过jdbc连接HIVESERVICE 创建表 import java.sql.Connection; import java.s ...

  8. ios开发之xcode6中如何添加pch全局引用文件

    xcode6中去掉了默认添加pch文件,这就需要我们自己手动添加pch文件了,添加pch文件是为了一些琐碎的头文件引用,加快编译速度! 下面就说下该如何手动添加pch文件: 1.添加一个文件,在oth ...

  9. 参数计数不匹配,未处理System.Reflection.TargetParameterCountException

    系统出现异常:参数计数不匹配,未处理System.Reflection.TargetParameterCountException, 系统会显示如下的异常信息,但异常信息往往与实际异常位置差十万八千量 ...

  10. PHP-HTML重要知识点笔记

    1.用frameset.frame和iframe还实现多窗口 2.在图片上利用映射距离usemap来实现按钮跳转.------第8尾集 3.表单必须要有name和value,因为抓包的时候,可发现必须 ...