New Year Permutation time limit per test2 seconds memory limit per
test256 megabytes inputstandard input outputstandard output User ainta
has a permutation p 1, p 2, …, p n. As the New Year is coming, he
wants to make his permutation as pretty as possible.Permutation a 1, a 2, …, a n is prettier than permutation b 1, b
2, …, b n, if and only if there exists an integer k (1 ≤ k ≤ n)
where a 1 = b 1, a 2 = b 2, …, a k - 1 = b k - 1 and a k < b k 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 p i and p j (1 ≤ i, j ≤ n, i ≠ j) if and only if A
i, j = 1.Given the permutation p and the matrix A, user ainta wants to know the
prettiest permutation that he can obtain.Input The first line contains an integer n (1 ≤ n ≤ 300) — the size of
the permutation p.The second line contains n space-separated integers p 1, p 2, …, p n
— 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. The j-th
character of the i-th line A i, 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, A i, j = A j, i holds. Also,
for all integers i where 1 ≤ i ≤ n, A i, i = 0 holds.Output In the first and only line, print n space-separated integers,
describing the prettiest permutation that can be obtained.Examples inputCopy 7 5 2 4 3 6 7 1 0001001 0000000 0000010 1000001
0000000 0010000 1001000 outputCopy 1 2 4 3 6 7 5 inputCopy 5 4 2 1 5 3
00100 00011 10010 01101 01010 outputCopy 1 2 3 4 5 Note In the first
sample, the swap needed to obtain the prettiest permutation is: (p
1, p 7).In the second sample, the swaps needed to obtain the prettiest
permutation is (p 1, p 3), (p 4, p 5), (p 3, p 4).A permutation p is a sequence of integers p 1, p 2, …, p n,
consisting of n distinct positive integers, each of them doesn’t
exceed n. The i-th element of the permutation p is denoted as p i. The
size of the permutation p is denoted as n.
中文版
新年排列 每个测试的时限2秒 每个测试的内存限制256 MB 输入标准输入 输出标准输出 用户ainta具有一个排列p 1,p
2,…,p n。随着新年的到来,他想让自己的排列尽可能漂亮。当且仅当存在一个整数k(1≤k≤n)且其中1 = b 1时,置换a 1,a 2,…,an比置换b 1,b 2,…,bn更漂亮。 ,a
2 = b 2,…,ak-1 = bk-1和ak <bk全部成立。众所周知,置换p非常敏感,以至于只能通过交换两个不同的元素进行修改。但是交换两个元素比您想象的要难。给定一个n××n个二进制矩阵A,用户ainta可以并且仅当A
i,j = 1时才交换p i和p j的值(1≤i,j≤n,i≠j)。给定排列p和矩阵A,用户ainta想要知道他可以获得的最漂亮的排列。
输入项 第一行包含整数n(1≤n≤300)-排列p的大小。
第二行包含n个以空格分隔的整数p 1,p 2,…,p n-用户ainta具有的排列p。 1和n之间的每个整数在给定的排列中恰好发生一次。
接下来的n行描述矩阵A。第i行包含n个字符“ 0”或“ 1”,并描述A的第i行。第i行A
i,j的第j个字符是保证A的第i行和第j列的交点上的元素。对于所有整数i,j,其中1≤i <j≤n,A i,j = A
j,i成立。而且,对于所有1 1≤i1≤n的整数i,A i,i i = 0。输出量 在第一行也是唯一的行中,打印n个以空格分隔的整数,描述可以获取的最漂亮的排列。
例子 inputCopy 7 5 2 4 3 6 7 1 0001001 0000000 0000010 1000001 0000000
0010000 1001000 outputCopy 1 2 4 3 6 7 5 inputCopy 5 4 2 1 5 3 00100
00011 10010 01101 01010 outputCopy 1 2 3 4 5 注意
在第一个样本中,获得最漂亮置换所需的交换是:(p 1,p 7)。在第二个样本中,获得最漂亮置换所需的交换是(p 1,p 3),(p 4,p 5),(p 3,p 4)。
排列p是整数p 1,p 2,…,p n的序列,由n个不同的正整数组成,每个整数不超过n。置换p的第i个元素表示为p
i。置换p的大小表示为n。
大概意思就是给你一个整型数组p,这个数组可进行无限次交换。
再给你一个n*n的二进制数组a,a=0,表示i到j不可达,反之可达。即当可达时i和j可以交换,否则不可以交换。题目要我们求出这个数组经过若干次交换后,使得数值小的元素放在数组的前面。所以,首先我们需要知道i到j是否可达,而且n<=500,故可以用floyd来进行一个预处理.
理清关系后,就不难写成代码了。
#include<iostream>
#include<cstring>
using namespace std;
int a[305][305];
int p[305];
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>p[i];
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
char c;
cin>>c;
a[i][j]=c-'0';
}
}
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(a[i][k]&&a[k][j])
a[i][j]=1;
}
}
}
for(int i=1;i<=n;i++){
int index=i;
for(int j=i+1;j<=n;j++){
if(a[i][j]&&p[j]<p[index])
index=j;
}
if(index!=i)swap(p[i],p[index]);
}
for(int i=1;i<=n;i++)
cout<<p[i]<<" ";
return 0;
}