解题思路:给出一列数an,再给出一个矩阵d[i][j],在满足d[i][j]=1的情况下,称a[i]和a[j]可以交换,问经过交换最后得到字典序最小的数列a[n]

首先是贪心的思想,大的能换就换到后面去,这样尽可能使小的在前面。

然后是判断a[i]和a[j]能不能交换, 如果d[i][j]=1,表示a[i]和a[j]可直接交换 如果d[i][k]=1&&d[k][j]=1,表示a[i]可以通过a[k]和a[j]交换

比如 1 7 3 4  2 5 假如 7能和4交换,4能和2交换,那么7也能和2交换

处理矩阵的时候用到了紫书中的传递闭包

for(k=;k<=n;k++)
for(i=;i<=n;i++)
for(j=;j<=n;j++)
d[i][j]=d[i][j]||(d[i][k]&&d[k][j]);
B. New Year Permutation
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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 ≤ ni ≠ 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.

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 p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1and 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 thei-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 ≤ nAi, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ nAi, i = 0 holds.

Output

In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained.

Sample test(s)
input
7 5 2 4 3 6 7 1 0001001 0000000 0000010 1000001 0000000 0010000 1001000
output
1 2 4 3 6 7 5
input
5 4 2 1 5 3 00100 00011 10010 01101 01010
output
1 2 3 4 5
Note

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. Thei-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n.

#include<stdio.h>
#include<algorithm>
using namespace std;
int a[1010],d[1010][1010];
char s[1020];
int main()
{
int n,i,j,k;
scanf("%d",&n);
for(i=1;i<=n;i++) scanf("%d",&a[i]);
for(i=1;i<=n;i++)
{
scanf("%s",&s);
for(j=0;j<n;j++)
{
if(s[j]=='1') d[i][j+1]=1;
else d[i][j+1]=0;
}
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
d[i][j]=d[i][j]||(d[i][k]&&d[k][j]); for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(d[i][j]&&a[i]>a[j])
swap(a[i],a[j]);
}
}
for(i=1;i<n;i++)
printf("%d ",a[i]);
printf("%d\n",a[i]);
}

  

Good Bye 2014 B. New Year Permutation 【传递闭包 贪心】的更多相关文章

  1. CodeForces Good Bye 2014 B. New Year Permutation

    可能是因为这次没有分Div.1和Div.2,所以感觉题的难度比较大. 题意: 给出一个1~n的排列和一个邻接矩阵A,Aij = 1表示可以交换排列的第i项和第j项,问经过若干次交换后,求能够得到最小字 ...

  2. Good Bye 2014 B. New Year Permutation(floyd )

    题目链接 题意:给n个数,要求这n个数字小的尽量放到前面,求一个最小的. 给一个矩阵s[i][j]==1,表示位置 i 的数字可以和 位置 j 的数字交换. 分析: 刚开始用的是3个循环,每次都找一个 ...

  3. 【BZOJ】2014: [Usaco2010 Feb]Chocolate Buying(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2014 这应该是显然的贪心吧,先排序,然后按花费取 #include <cstdio> # ...

  4. Hello world,Hello 2015,Bye 2014

    序 在我写下“在”这个字的时候已经是2014-12-31 19:59,14年最后一天了,总觉得不写点东西祭奠一下那些被自己虐死的脑细胞,心里就不舒服. 那就从生活,工作,学习三个方面,总结一下吧. 生 ...

  5. Good Bye 2014 D. New Year Santa Network 图论+期望

    D. New Year Santa Network   New Year is coming in Tree World! In this world, as the name implies, th ...

  6. Good Bye 2014 F - New Year Shopping

    F - New Year Shopping 对于一种特殊的不可逆的dp的拆分方法.. 也可以用分治写哒. #include<bits/stdc++.h> #define LL long l ...

  7. Good Bye 2014 E - New Year Domino 单调栈+倍增

    E - New Year Domino 思路:我用倍增写哒,离线可以不用倍增. #include<bits/stdc++.h> #define LL long long #define f ...

  8. 2014 Multi-University Training Contest 1/HDU4864_Task(贪心)

    解题报告 题意,有n个机器.m个任务. 每一个机器至多能完毕一个任务.对于每一个机器,有一个最大执行时间Ti和等级Li,对于每一个任务,也有一个执行时间Tj和等级Lj.仅仅有当Ti>=Tj且Li ...

  9. Codeforces 500B New Year Permutation( Floyd + 贪心 )

    B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

随机推荐

  1. Memcache相关面试题

    1)memcached的cache机制是怎样的? 懒惰算法 +最近最少使用原则 2)memcached如何实现冗余机制? 冗余:就是有好多好多不经常使用的. 可以不用实现冗余机制,如果非要实现.那就搞 ...

  2. 大数据量.csv文件导入SQLServer数据库

    前几天拿到了一个400多M的.csv文件,在电脑上打开要好长时间,打开后里面的数据都是乱码.因此,做了一个先转码再导入数据库的程序.100多万条的数据转码+导入在本地电脑上花了4分钟,感觉效率还可以. ...

  3. Arduino扫盲(持续添加中)

    1.Arduino火的很,很大一点在于,他基本透明掉了硬件电子部分,只剩下软件部分,通过把电子部分包装成黑箱,使得大量IT人士,普通人,甚至小学生也能玩的来. 2 .Arduino是一个电子原型开发平 ...

  4. 路飞学城Python-Day36

    24-记录的增删改查 1. 插入完整数据(顺序插入) 语法一: INSERT INTO 表名(字段1,字段2,字段3…字段n) VALUES(值1,值2,值3…值n);   语法二: INSERT I ...

  5. 路飞学城Python-Day29(第四模块-并发编程)

    01-进程与程序的概念 并发:多进程和多线程 进程的概念:进程就是正在执行的过程,一个应用程序不是进程,只有应用程序启动以后才能说是进程,进程是一个抽象的概念,起源于操作系统 02-操作系统介绍 应用 ...

  6. P3376 【模板】网络最大流(luogu)

    P3376 [模板]网络最大流(luogu) 最大流的dinic算法模板(采取了多种优化) 优化 时间 inline+当前弧+炸点+多路增广 174ms no 当前弧 175ms no 炸点 249 ...

  7. crm 系统项目(三) 业务

    1. 项目背景 crm系统是某教育平台正在使用的项目,系统主要为 销售部.运营部.教质部门提供管理平台,随着公司规模的扩展,对公司员工的业务信息量化以及信息化建设越来越重要. crm系统为不同角色的用 ...

  8. Redis:持久化之RDB和AOF

    Redis:持久化之RDB和AOF RDB(Redis DataBase) 在指定的时间间隔内将内存中的数据集快照写入硬盘 也就是行话讲的Snapshot快照,它恢复时是将快照文件直接读到内存里. R ...

  9. 接口测试及接口Jmeter工具介绍

    一.接口类型及数据传递的格式 接口类型: 1.HTTP接口:通过GET或POST来获取数据,在数据处理上效率比较高 2.WebServer接口:通过SOAP协议来获取数据,比起http来说处理更加复杂 ...

  10. 异构关系数据库(MySql与Oracle)之间的数据类型转换参考

    一.MySQL到Oracle的数据类型的转变: 编号 MySQL ToOracle Oracle 1 GEOMETRY BLOB BLOB 2 GEOMETRYCOLLECTION BLOB BLOB ...