Good Bye 2014 B. New Year Permutation 【传递闭包 贪心】
解题思路:给出一列数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]);
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 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 ≤ 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. 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 【传递闭包 贪心】的更多相关文章
- CodeForces Good Bye 2014 B. New Year Permutation
可能是因为这次没有分Div.1和Div.2,所以感觉题的难度比较大. 题意: 给出一个1~n的排列和一个邻接矩阵A,Aij = 1表示可以交换排列的第i项和第j项,问经过若干次交换后,求能够得到最小字 ...
- Good Bye 2014 B. New Year Permutation(floyd )
题目链接 题意:给n个数,要求这n个数字小的尽量放到前面,求一个最小的. 给一个矩阵s[i][j]==1,表示位置 i 的数字可以和 位置 j 的数字交换. 分析: 刚开始用的是3个循环,每次都找一个 ...
- 【BZOJ】2014: [Usaco2010 Feb]Chocolate Buying(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=2014 这应该是显然的贪心吧,先排序,然后按花费取 #include <cstdio> # ...
- Hello world,Hello 2015,Bye 2014
序 在我写下“在”这个字的时候已经是2014-12-31 19:59,14年最后一天了,总觉得不写点东西祭奠一下那些被自己虐死的脑细胞,心里就不舒服. 那就从生活,工作,学习三个方面,总结一下吧. 生 ...
- 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 ...
- Good Bye 2014 F - New Year Shopping
F - New Year Shopping 对于一种特殊的不可逆的dp的拆分方法.. 也可以用分治写哒. #include<bits/stdc++.h> #define LL long l ...
- Good Bye 2014 E - New Year Domino 单调栈+倍增
E - New Year Domino 思路:我用倍增写哒,离线可以不用倍增. #include<bits/stdc++.h> #define LL long long #define f ...
- 2014 Multi-University Training Contest 1/HDU4864_Task(贪心)
解题报告 题意,有n个机器.m个任务. 每一个机器至多能完毕一个任务.对于每一个机器,有一个最大执行时间Ti和等级Li,对于每一个任务,也有一个执行时间Tj和等级Lj.仅仅有当Ti>=Tj且Li ...
- Codeforces 500B New Year Permutation( Floyd + 贪心 )
B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
随机推荐
- JS进阶 - 浏览器工作原理
一.浏览器的结构 浏览器的主要组件为: 用户界面 - 包括地址栏.前进/后退按钮.书签菜单等.除了浏览器主窗口(显示页面),其他部分都属于用户界面. 浏览器引擎 - 在用户界面和渲染引擎之间传送指令. ...
- 维基百科 MediaWiki API 解析
使用开放的 API 做一个自己的小项目,是一个很好的学习方法.但好像开放的 API 选择并不多.这里给大家多一个选择,简单介绍一下维基百科使用的 MediaWiki API. 简介 先简单介绍几个容易 ...
- here.less
<html><head><title>Test Less</title><link rel="stylesheet/less" ...
- 04《UML大战需求分析》之四
在学习完顺序图之后,流程分析的三种图,我已经学习完了我,但是我还需要大量地锻炼,这样才可以更加熟练地掌握几种图的使用和方法.接下来,我学习了用例图,用来描述系统的行为. 虽然是一同学习的,但是对用例图 ...
- ZBrush中如何实现智能对称
ZBrush软件智能化和人性化的工作流程让用户在创作中提高工作效率,体验创作乐趣,说起智能化不得不提的就是ZBrush 4R8®给我们提供的智能对称功能,所谓的智能对称就是当您在编辑其中一半的物体模型 ...
- jsp基本内置标签
<jsp:foward page="url"> <jsp:param value=" " name=" "/> &l ...
- JS中的与冒号的作用、箭头函数相关的一道题
相关知识来自于一道题: 使用ES6的箭头函数语法可以直接省略 function 和 return 关键字,比如 function (){return 1;} 就可以简化成 () => 1 , 但 ...
- BZOJ 5020 [THUWC2017]Drown in the math ocean (LCT+求导)
题目大意: 太长了略 洛谷题面传送门 嗯,数学题 感觉考试要是出这种题我就死翘翘了[逃 不用想都知道要$LCT$维护断边连边,但询问该如何处理呢 利用题目给出的公式 $f(x)=\sum_{i=0}^ ...
- JQuery封装ajax的方法
1.$.post方法 $.post(url[,data][,callback][,type]) url:请求的后台程序地址 data:发送到后台的数据 callback:载入成功时回调函数,该函数参数 ...
- Having用法
HAVING 子句对 GROUP BY 子句设置条件的方式与 WHERE 和 SELECT 的交互方式类似.WHERE 搜索条件在进行分组操作之前应用:而 HAVING 搜索条件在进行分组操作之后应用 ...