CF 500B New Year Permutation
题目大意
给你一个数列,再给你一个矩阵,矩阵的(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的更多相关文章
- Codeforces 500B. New Year Permutation[连通性]
B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- CF 1141C Polycarp Restores Permutation
Description An array of integers p1,p2,…,pnp1,p2,…,pn is called a permutation if it contains each nu ...
- Codeforces 500B New Year Permutation( Floyd + 贪心 )
B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- codeforces 500B.New Year Permutation 解题报告
题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...
- cf B. Levko and Permutation
http://codeforces.com/contest/361/problem/B #include <cstdio> #include <cstring> #includ ...
- [CodeForces]500B New Year Permutation
刷水题做几道入门贪心题预热... 找联通块里字典序最小的放到最前面即可.记得写传递闭包 #include <iostream> #include <cstdio> #inclu ...
- 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 ...
- 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 ...
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) E. Jeff and Permutation
http://codeforces.com/contest/351/problem/E 题意: 给出一些数,可以改变任意数的正负,使序列的逆序对数量最少 因为可以任意加负号,所以可以先把所有数看作正数 ...
随机推荐
- C#窗体代码相关笔记
获取ComboBox下拉列表的所有选项值 ArrayList al = new ArrayList(); foreach (string item in this.comboBox2.Items) { ...
- (转)FastCgi与PHP-fpm之间是个什么样的关系
首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. web server(比如说nginx)只是内容的分发者.比如,如果请求/index.h ...
- 洛谷P4514 上帝造题的七分钟
P4514 上帝造题的七分钟 题目背景 裸体就意味着身体. 题目描述 "第一分钟,X说,要有矩阵,于是便有了一个里面写满了000的n×mn×mn×m矩阵. 第二分钟,L说,要能修改,于是便有 ...
- 安装zabbix需要php的两个模块php-bcmath和php-mbstring(转)
安装zabbix需要php的两个模块php-bcmath和php-mbstring 原创 Linux操作系统 作者:甲骨文技术支持 时间:2018-02-24 18:35:24 1472 0 1. ...
- git 命令行(四)-推送分支到远程
在本地新建一个分支: git branch newBranch 切换到你的新分支: git checkout newBranch 创建并切换到新分支: git checkout -b newBranc ...
- 深入浅出 Java Concurrency (37): 并发总结 part 1 死锁与活跃度[转]
死锁与活跃度 前面谈了很多并发的特性和工具,但是大部分都是和锁有关的.我们使用锁来保证线程安全,但是这也会引起一些问题. 锁顺序死锁(lock-ordering deadlock):多个线程试图通 ...
- <scrapy爬虫>爬取腾讯社招信息
1.创建scrapy项目 dos窗口输入: scrapy startproject tencent cd tencent 2.编写item.py文件(相当于编写模板,需要爬取的数据在这里定义) # - ...
- Jmeter安装与配置(第一篇)
Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试领域. 它可以用于测试静态和动态资源,例如静态文 ...
- Qt源码下载
这是官方下载地址:http://qt.nokia.com/downloads 点击右下角的 ftp.qt.nokia.com - Archive You can find our archive of ...
- [转]WPF 构建无外观(Lookless)控件
构建一个用户可以使用Template属性设置外观的WPF控件需要以下几步 1.继承自System.Windows.Controls.Control 2.设置DefaultStyleKeyPropert ...