题目链接:

  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. TOM大师脚本-show space 多个版本,谢谢大牛们

    示例一 该脚本需区分 对象的管理方式是 自动还是 手动, 对手动管理方式 的表显示很全面 SQL> exec show_space_old('MAN_TAB','DEV','TABLE'); F ...

  2. 文字排版--字号、颜色(font-size, color)

    可以使用下面代码设置网页中文字的字号为12像素,并把字体颜色设置为#666(灰色): body{font-size:12px;color:#666} 示例: <!DOCTYPE HTML> ...

  3. modifytime是一个神奇的column name----这边文章是错的totally,因为我的实验不彻底。timestamp属性很神奇,头一个timestamp,会自动的成DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

    在mysql里边modifytime是一个神奇的column name,试一下. 请执行sql语句 CREATE TABLE `test_time` ( `modifytime` timestamp ...

  4. vijos P1055奶牛浴场&& Winter Camp2002

    这道题是我在寒假的模拟赛里碰到的,现在想起来仍觉得余味无穷.题目大意大致如下:给你一个矩形并在其中划出一个最大的子矩形,当然,在这个矩形里有些地方是取不到的,也就是说我们划的这个子矩形不能包含这些点( ...

  5. 关于alarm函数

    #include<unistd.h> #include<signal.h> void handler() { printf("Hello\n"); sign ...

  6. 得到某个进程所有线程ID和入口地址

    #include <windows.h> #include <tlhelp32.h> #include "iostream" using namespace ...

  7. IFrame中Session丢失的解决办法

    1.打开IIS管理器 inetmgr2.选择被嵌入iframe源站点或者目录,右键点击打开属性框3.切换到HTTP头4.添加5.自定义HTTP头名: P3P6.自定义HTTP头值: CP=" ...

  8. [转]加盐hash保存密码的正确方式

    0x00 背景 大多数的web开发者都会遇到设计用户账号系统的需求.账号系统最重要的一个方面就是如何保护用户的密码.一些大公司的用户数据库泄露事件也时有发生,所以我们必须采取一些措施来保护用户的密码, ...

  9. Unity3d webplayer发布的问题和100%自适应浏览器

    Unity3d发布的问题 发布的时候,遇到无法写入Resources.assets,原来是我的项目中有多个Resources文件夹,以后最好是不要有重复的文件夹和一样名字的资源! 还有就是发布的web ...

  10. Frequent values

    poj3368:http://poj.org/problem?id=3368 题意:给你一个非下降的序列,然后查询[l,r]内出现最多数字的次数. 题解:首先,因为序列是非下降的,所以相同的数字出现在 ...