http://codeforces.com/contest/500/problem/B
题意:给定一个1至n的数的一种排列。给定一个n*n的矩阵,a[i][j]==0代表pi,pj不可以交换,a[i][j]为1代表p[i],p[j]可以交换。 问字典序最小的排列。。
思路:把矩阵看成图的关系。。反正n很小。。跑一遍floyd..得到可以间接交换的点。。然后冒泡排序就好。。只有p[i]>p[j]并且a[i][j]的时候交换。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
/* *********************************************** Author :111qqz Created Time :2015年12月05日 星期六 15时00分14秒 File Name :code/cf/problem/500B.cpp ************************************************ */ #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <cmath> #include <cstdlib> #include <ctime> #define fst first #define sec second #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define ms(a,x) memset(a,x,sizeof(a)) typedef long long LL; using namespace std; const double eps = 1E-8; const int dx4[4]={1,0,0,-1}; const int dy4[4]={0,-1,1,0}; const int inf = 0x3f3f3f3f; const int N=305; char ok[N][N]; int p[N]; int n; int a[N][N]; void print() { for ( int i = 0 ; i < n ;i++) printf("%d ",p[i]); } void floyd() { for ( int k = 0 ; k < n ;k ++) for ( int i = 0 ; i < n ; i++) for ( int j = 0 ; j < n ; j++) if (a[i][j]==-1&&a[i][k]==1&&a[k][j]==1) a[i][j] = 1; } int main() { #ifndef ONLINE_JUDGE freopen("code/in.txt","r",stdin); #endif scanf("%d",&n); ms(a,-1); for ( int i = 0 ; i < n ; i++) scanf("%d",&p[i]); for ( int i = 0 ; i < n ; i++) scanf("%s",ok[i]); for ( int i = 0 ; i < n ; i ++) { for ( int j = 0 ; j < n ; j++) { if (ok[i][j]=='1') { a[i][j] = 1; // a[j][i] = 1; } } } floyd(); for ( int i = 0 ; i < n-1 ; i++) for ( int j = i+1 ; j < n ;j++) if (p[i]>p[j]&&a[i][j]==1) { swap(p[i],p[j]); } print(); #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; } |
说点什么
您将是第一位评论人!