Codeforces 500B New Year Permutation( Floyd + 贪心 )
2 seconds
256 megabytes
standard input
standard output
User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible.
Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all holds.
As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ n, i ≠ j) if and only if Ai, j = 1.
Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain.
The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p.
The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation.
Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. Thej-th character of the i-th line Ai, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds.
In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained.
7
5 2 4 3 6 7 1
0001001
0000000
0000010
1000001
0000000
0010000
1001000
1 2 4 3 6 7 5
5
4 2 1 5 3
00100
00011
10010
01101
01010
1 2 3 4 5
In the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7).
In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4).
A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n.
题意:序列 a1, a2, ..., an , b1, b2, ..., bn, 如果存在整数 k (1 ≤ k ≤ n) 符合 a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 并且 ak < bk .
则称 a 序列比 b序列好。
题目给出一个序列 p ,再给出一个n*n的矩阵A , A[i][j] = 1 ,表示p中第i个元素能跟第j个元素交换 。
问p经过交换后,最好的序列是什么。
做法是用Floyd先处理任意pi , pj 能否交换 O(n^3)
然后再将序列从前到后贪心交换一次。
#include <bits/stdc++.h>
using namespace std;
const int N = ;
int n , a[N] , A[N][N];
string s ;
int main()
{
cin >> n;
for( int i = ; i <= n ; ++i ) cin >> a[i];
for( int i = ; i <= n ; ++i ) {
cin >> s ;
for( int j = ; j < n ; ++j ){
if( s[j] == '' ) A[i][j+] = ;
else A[i][j+] = ;
}
}
for( int i = ; i <= n ; ++i ){
for( int j = ; j <= n ; ++j ){
if( A[j][i] == ) continue ;
for( int k = ; k <= n ; ++k ){
if( A[i][k] ) A[j][k] = ;
}
}
} for( int i = ; i <= n ; ++i ){
for( int j = i + ; j <= n ; ++j ){
if( A[i][j] && a[i] > a[j] ) swap( a[i] , a[j] );
}
}
for( int i = ; i <= n ; ++i ) cout << a[i] << ' ';
}
Codeforces 500B New Year Permutation( Floyd + 贪心 )的更多相关文章
- Codeforces 500B. New Year Permutation[连通性]
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.紧接着是一个 ...
- [CodeForces]500B New Year Permutation
刷水题做几道入门贪心题预热... 找联通块里字典序最小的放到最前面即可.记得写传递闭包 #include <iostream> #include <cstdio> #inclu ...
- Codeforces 437C The Child and Toy(贪心)
题目连接:Codeforces 437C The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- CodeForces 124C Prime Permutation (数论+贪心)
题意:给定一个字符串,问你能不能通过重排,使得任意一个素数p <= 字符串长度n,并且 任意的 i <= 长度n/素数p,满足s[p] == s[p*i]. 析:很容易能够看出来,只要是某 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心
C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...
- Educational Codeforces Round 2 C. Make Palindrome 贪心
C. Make Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...
随机推荐
- 金蝶云k3 cloud采购入库单校验日期不通过
新增采购入库单的时候提示单据日期必须大于等于货主组织在核算系统最后关账日期 解决办法:库存系统和存货核算系统的反关账
- JS面向对象——组合使用构造函数模型与原型模型
该模型为创建自定义类型最常用的方式. <!DOCTYPE html> <html> <head> <title>组合使用构造函数模型和原型模型</ ...
- Tomcat的用途
总结: 这篇文章主要反思了Tomcat的作用.本文主要是自己的一个思考过程,不是严谨地介绍和详细总结Tomcat使用方法的文章.最后尝试利用tomcat的知识,以URL的形式来访问文件夹(在浏览器的地 ...
- spring cloud学习一--Eureka服务注册与发现
spring cloud Eureka是基于Netflix Eureka服务发现注册产品的二次封装,它提供了服务注册功能(Service Registry)和服务发现功能(Service Discov ...
- web项目使用fastdsf上传|下载文件
在上传代码中添加一下代码 suffix=suffix.substring(1); fast.FastDFSFile file = new fast.FastDFSFile(mFile.getBytes ...
- python+django+pycharm 环境配置 (window7)
一.python环境配置 登录python官网,下载windows版的python,本项目使用32位的python2.7.6,下载地址: http://www.python.org/ftp/pytho ...
- 数组之间的比较应当用Arrays.equals()
被坑了,数组之间的比较不能用“==”,应当用Arrays.equals() 如果是原生数组(即数组中的值是几大基本数据类型之一的)之间的比较可以直接用,如果数组中的值不是原生的基本数据类型,那么再使用 ...
- Spring---基础配置
1.@Scope 1.1.描述了Spring容器如何新建Bean的实例: 1.2.@Scope(value="") value值有: 1.2.1.singleton 一个Sprin ...
- SpringIntegration---Redis
1.依赖 <dependency> <groupId>org.springframework.integration</groupId> <artifactI ...
- head、tail 命令和实时(real-time)更新
head.tail 命令和实时(real-time)更新 head filename: 输出 filename 文件的前10项 tail filename: 输出 filename 文件的后10项 即 ...