传送门

题目大意

给你一个数列,再给你一个矩阵,矩阵的(i,j)如果为1就表示可以将i,j位置上的数交换,问任意交换之后使原数列字典序最小并输出。

解题思路

因为如果i与j能交换,j与k能交换,那么i与k相当于能直接交换,所以我们先使用传递闭包求出所有可以交换的情况。之后从第一个位置开始贪心,看它能跟后面哪个小于它的数交换。

代码

#include<iostream>
#include<cstdio>
#include<cstring> using namespace std;
const int MAXN = 305; int dp[MAXN][MAXN];
int n,st[MAXN]; inline void floyd(){
for(register int k=1;k<=n;k++)
for(register int i=1;i<=n;i++)
for(register int j=1;j<=n;j++)
dp[i][j]|=dp[i][k]&dp[k][j];
} int main(){
scanf("%d",&n);
for(register int i=1;i<=n;i++) scanf("%d",&st[i]);
for(register int i=1;i<=n;i++){
char c[MAXN];
scanf("%s",c+1);
for(register int j=1;j<=n;j++)
dp[i][j]=c[j]-'0';
}
floyd();
for(register int i=1;i<=n;i++)
for(register int j=i+1;j<=n;j++)
if(dp[i][j] && st[j]<st[i])
swap(st[j],st[i]);
for(register int i=1;i<=n;i++) printf("%d ",st[i]);
return 0;
}

CF 500B New Year Permutation的更多相关文章

  1. Codeforces 500B. New Year Permutation[连通性]

    B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  2. CF 1141C Polycarp Restores Permutation

    Description An array of integers p1,p2,…,pnp1,p2,…,pn is called a permutation if it contains each nu ...

  3. Codeforces 500B New Year Permutation( Floyd + 贪心 )

    B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  4. codeforces 500B.New Year Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...

  5. cf B. Levko and Permutation

    http://codeforces.com/contest/361/problem/B #include <cstdio> #include <cstring> #includ ...

  6. [CodeForces]500B New Year Permutation

    刷水题做几道入门贪心题预热... 找联通块里字典序最小的放到最前面即可.记得写传递闭包 #include <iostream> #include <cstdio> #inclu ...

  7. HDU 4951 Multiplication table 阅读题

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4951 题意:给一个P进制的乘法表.行和列分别代表0~p-1,第i行第j*2+1和第j*2+2列代表的是第i ...

  8. CF 500 B. New Year Permutation 并查集

    User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permut ...

  9. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) E. Jeff and Permutation

    http://codeforces.com/contest/351/problem/E 题意: 给出一些数,可以改变任意数的正负,使序列的逆序对数量最少 因为可以任意加负号,所以可以先把所有数看作正数 ...

随机推荐

  1. java笔试之字符逆序(二)

    与字符逆序(一)不同,句子逆序,单词顺序.例如:I am a student 输出为student a am I. 想法:先字符串句子逆序,然后针对每个单词再逆序一遍. package test; i ...

  2. 2018-8-10-win10-uwp-如何判断一个对象被移除

    title author date CreateTime categories win10 uwp 如何判断一个对象被移除 lindexi 2018-08-10 19:16:50 +0800 2018 ...

  3. [转]成为Java顶尖程序员 ,看这11本书就够了

    “学习的最好途径就是看书“,这是我自己学习并且小有了一定的积累之后的第一体会.个人认为看书有两点好处: 1.能出版出来的书一定是经过反复的思考.雕琢和审核的,因此从专业性的角度来说,一本好书的价值远超 ...

  4. Ignite-Spark

    2017.12.02 Ignite-Spark 讲ppt的时候还是很紧张 PPT地址 https://files.cnblogs.com/files/swobble/Spark.pptx

  5. (转载)——Centos下安装Redis(原文地址:http://www.nnzhp.cn/archives/169)

    原文地址:http://www.nnzhp.cn/archives/169 今天介绍一下redis,重点介绍一下redis的安装. Redis 是一个基于内存的高性能key-value数据库,数据都保 ...

  6. UNION操作用于合并两个或多个 SELECT 语句的结果集。

    UNION操作用于合并两个或多个 SELECT 语句的结果集. 大理石平台价格 使用示例: $Model->field('name') ->table('think_user_0') -& ...

  7. MyEclipse如何使用debug模式

    知道如何打断点,如何进入debug与debug模式的视图,还有工具栏的使用和快捷键的使用 https://blog.csdn.net/menglanyingfei/article/details/55 ...

  8. ConcurrentHashMap线程安全的具体实现方式/底层具体实现

    1. jdk1.7以及之前 ConcurrentHashMap 是由 Segment 数组结构和 HashEntry 数组结构组成. 通俗的话讲:就是首先将数据分为一段一段的存储,然后给每一段数据配一 ...

  9. Git婴幼儿使用手册【十分钟让你帅气的使用命令行和团队工作】

    Git由来:...... Git使用的好处:...... 如何使用Git:(以上会显得我们以下的是很纯纯的干货) 代码库有两个部分: 本地代码库:远程代码库: 本地代码库使用方法: 一.先创建一个文件 ...

  10. C#多线程实现方法——线程池(Thread Pool)

    ThreadPool使用 同步机制   ThreadPool使用 需要定义waitcallback委托形式如 public delegate void WaitCallback(object stat ...