可能是因为这次没有分Div.1和Div.2,所以感觉题的难度比较大。

题意:

给出一个1~n的排列和一个邻接矩阵A,Aij = 1表示可以交换排列的第i项和第j项,问经过若干次交换后,求能够得到最小字典序的排列。

分析:

如果a和b可交换,b和c可交换,则a和c也可以交换位置。如果把这n个位置看做顶点,两个可交换的位置连一条边,则图中在同一连通分量的顶点都是可以交换元素的。所以用并查集做就很方便了。

要想得到字典序最小的排列,直接贪心就可以了。从第一个数开始,首先试试1能不能交换到第一个位置去,否则尝试2,一直到能交换或者没有比开头的数更小的数位置。然后继续尝试第二个数。代码中有个used标记数组,标记这个数是否在前面用过。

 #include <cstdio>
#include <algorithm> const int maxn = + ;
char G[maxn][maxn];
int p[maxn], a[maxn], pos[maxn];//pos记录每个数的位置
bool used[maxn];//标记每个数是否用过 int GetParent(int x)
{
return (p[x] == x ? x : p[x] = GetParent(p[x]));
} void Union(int x, int y)
{
int px = GetParent(x), py = GetParent(y);
if(px != py)
p[px] = py;
} int main()
{
//freopen("in.txt", "r", stdin);
int n;
scanf("%d", &n);
for(int i = ; i <= n; ++i) p[i] = i;
for(int i = ; i <= n; ++i) scanf("%d", &a[i]);
for(int i = ; i <= n; ++i) scanf("%s", G[i] + ); for(int i = ; i <= n; ++i) pos[a[i]] = i; for(int i = ; i <= n; ++i)
for(int j = ; j <= n; ++j)
if(G[i][j] == '') Union(i, j); for(int i = ; i < n; ++i)
{
used[a[i]] = true;
for(int j = ; j < a[i]; ++j)
{
if(used[j]) continue;
if(GetParent(i) == GetParent(pos[j]))
{
used[a[i]] = false;
used[j] = true;
int q = pos[j];
std::swap(a[i], a[q]); //交换两元素
std::swap(pos[a[i]], pos[a[q]]); //同时交换每个数的位置
break;
}
}
} for(int i = ; i < n; ++i) printf("%d ", a[i]);
printf("%d\n", a[n]); return ;
}

代码君

CodeForces Good Bye 2014 B. New Year Permutation的更多相关文章

  1. Good Bye 2014 B. New Year Permutation 【传递闭包 贪心】

    解题思路:给出一列数an,再给出一个矩阵d[i][j],在满足d[i][j]=1的情况下,称a[i]和a[j]可以交换,问经过交换最后得到字典序最小的数列a[n] 首先是贪心的思想,大的能换就换到后面 ...

  2. Good Bye 2014 B. New Year Permutation(floyd )

    题目链接 题意:给n个数,要求这n个数字小的尽量放到前面,求一个最小的. 给一个矩阵s[i][j]==1,表示位置 i 的数字可以和 位置 j 的数字交换. 分析: 刚开始用的是3个循环,每次都找一个 ...

  3. Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)

    题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...

  4. Codeforces Round #275 (Div. 1)A. Diverse Permutation 构造

    Codeforces Round #275 (Div. 1)A. Diverse Permutation Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 ht ...

  5. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  6. 构造 Codeforces Round #275 (Div. 2) C. Diverse Permutation

    题目传送门 /* 构造:首先先选好k个不同的值,从1到k,按要求把数字放好,其余的随便放.因为是绝对差值,从n开始一下一上, 这样保证不会超出边界并且以防其余的数相邻绝对值差>k */ /*** ...

  7. Codeforces Good Bye 2018 D (1091D) New Year and the Permutation Concatenation

    题意:给n!个n的排列,按字典序从小到大连成一条序列,例如3的情况为:[1,2,3, 1,3,2, 2,1,3 ,2,3,1 ,3,1,2 ,3,2,1],问其中长度为n,且和为sum=n*(n+1) ...

  8. Codeforces:Good Bye 2018(题解)

    Good Bye 2018! 题目链接:https://codeforces.com/contest/1091 A. New Year and the Christmas Ornament 题意: 给 ...

  9. codeforces Good Bye 2013 379D New Year Letter

    题目链接:http://codeforces.com/problemset/problem/379/D [题目大意] 告诉你初始字符串S1.S2的长度和递推次数k, 使用类似斐波纳契数列的字符串合并的 ...

随机推荐

  1. 淘宝的ip地址库

    1. 请求接口(GET): http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串] 2. 响应信息: (json格式的)国家 .省(自治区或直辖市 ...

  2. 在centos 6.4下安装opencv 2.3.1

    系统环境介绍: centos 6.4 1.安装依赖包 yum install cmake gcc gcc-c++ gtk+-devel gimp-devel gimp-devel-tools gimp ...

  3. WPF 概述

    WPF 全称是:Windows Presentation Foundation,直译为Windows表示基础.WPF是专门为GUI(Graphic User Interface)程序开发设计的. 在过 ...

  4. Educational Codeforces Round 12 E. Beautiful Subarrays 预处理+二叉树优化

    链接:http://codeforces.com/contest/665/problem/E 题意:求规模为1e6数组中,连续子串xor值大于等于k值的子串数: 思路:xor为和模2的性质,所以先预处 ...

  5. mac 下 配置 xhprof

    1: 下载 安装 xhprof wget http://pecl.php.net/get/xhprof-0.9.3.tgztar zxf xhprof-0.9.3.tgzcd xhprof-0.9.3 ...

  6. phpstorm运行在浏览器中执行php文件报502错误

    原因是之前mac自带的php5.5版本被我升级到了5.6 通过phpinfo()查看到目前php5.6的安装目录 重新制定一些interpreter的路径 /usr/local/php5/bin 就可 ...

  7. C#设计模式学习资料--观察者模式

    http://www.cnblogs.com/promise-7/archive/2012/05/14/2500759.html http://www.cnblogs.com/zhenyulu/art ...

  8. Delphi XE5 android toast

    unit Android.JNI.Toast; // Java bridge class imported by hand by Brian Long (http://blong.com) inter ...

  9. Csharp volatile 关键字

    volatile 关键字指示一个字段可以由多个同时执行的线程修改.声明为 volatile 的字段不受编译器优化(假定由单个线程访问)的限制.这样可以确保该字段在任何时间呈现的都是最新的值. vola ...

  10. ural 1108

    大数乘法    不会java   比赛的时候各种细节RE  WA  ........ #include <cstdio> #include <cstring> #include ...