传送门

题目大意

给你一个数列,再给你一个矩阵,矩阵的(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. vue 返回上一页

    参考:https://www.cnblogs.com/chenguiya/p/9118265.html 注:需为history模式 方法一: @click="back" back( ...

  2. AWS云管理平台

  3. Linux安装Java与Eclipse

    Linux安装Java和Eclipse 一.准备工作 1.下载jdk  https://www.oracle.com/technetwork/java/javase/downloads/jdk8-do ...

  4. [CF587-F]WI-FI

    显然DP题... f[i][0]表示这个点不装路由器,f[i][1]表示装路由器 转移也很简单,在前面一段区间找最小值就好了 但是直接转移是$O(n*k)$的,会T掉 大佬说这个东西有单调性,但是菜鸡 ...

  5. bootstrap table 实现固定悬浮table 表头并可以水平滚动

    在开发项目中,需要将表格头部固定,而且表格大多数情况下是会水平滚动的.项目的css框架是bootstrap 3,故也可以叫做bootstrap table. 需要实现的是:表格头部固定,并且支持水平滚 ...

  6. Loadrunner学习---脚本编写(1)

    Loadrunner学习---脚本编写(1) 中午看了两集<奋斗>发现越看越想看,但是想到好不容易没上班,在家还是赶紧学习下LR的知识吧.下面这个网页的文章原来也是看过的,但发现没几天就忘 ...

  7. 安卓手机端微信网页浏览记录清理debugx5.qq.com

    最近我们环境从复)星(云切换到阿里云.早上地铁路上就有小伙伴@,一阵搜索.找的如下的方法. 记录一下: 目前只支持安卓手机的微信内置浏览器清理. 由腾讯提供的网址http://debugx5.qq.c ...

  8. mui.fire()用法

    作用: mui.fire() 可以触发目标窗口的自定义事件 mui.fire( 目标窗口的webview , '自定义事件名' ,{参数列表}:) 目标窗口监听这个自定义事件 window.addEv ...

  9. hive设置列头(永久模式)

    到hive目录下的hive-site <property> <name>hive.cli.print.header</name> <value>true ...

  10. matlab 实现感知机线性二分类算法(Perceptron)

    感知机是简单的线性分类模型 ,是二分类模型.其间用到随机梯度下降方法进行权值更新.参考他人代码,用matlab实现总结下. 权值求解过程通过Perceptron.m函数完成 function W = ...