解题思路:给出一列数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. 又一个设计工具 Framer X Preview

    又一个设计工具 Framer X Preview 所说比 Sketch 和 Figma 都要好用,不知道是不是真的 ~~ To give you a little background, I've b ...

  2. Hibernate框架学习(七)——多对多关系

    一.关系表达 1.表中的表达 2.实体中的表达 3.orm元数据中的表达 在User.hbm.xml中添加: 在Role.hbm.xml中添加(与上相反): 二.操作关联属性 1.保存员工及角色 pu ...

  3. Unity 指定区域随机实例化预制体Prefab 代码

    using UnityEngine; public class NewBehaviourScript : MonoBehaviour { public GameObject prefab; void ...

  4. Unity 导入的模型检测不到碰撞

    解决方案 添加Mesh Collider和rigidbody,并且Collider里边2个勾要勾上. 这是我碰到的问题.如果没解决你的问题,别打我,逃~

  5. 脚本_使用expect自动交互远程主机安装软件

    #!bin/bash#功能:使用expect工具自动交互密码,远程到其它主机,安装httpd软件#作者:liusingbon#删除~/.ssh/known-hosts后,ssh远程任何主机,系统都会询 ...

  6. Hihocoder1458-Parentheses Matching(stack,vector)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given a string of balanced parentheses output all the matchin ...

  7. IPv6地址分配

  8. layui动态修改select的选中项

    layui动态修改select的选中项:(在layUI下给select设置默认选项) 例: $("select[name='result']").val(11); //重新渲染表单 ...

  9. luogo p3379 【模板】最近公共祖先(LCA)

    [模板]最近公共祖先(LCA) 题意 给一个树,然后多次询问(a,b)的LCA 模板(主要参考一些大佬的模板) #include<bits/stdc++.h> //自己的2点:树的邻接链表 ...

  10. hp soap扩展最全说明,附天气预报调用的例子

    自从php5开始,我们可以不用通过php nusoap来创建php soap web service 和调用 soap了,php5内置了 soap扩展.只需要在php.ini中开启soap的exten ...