Codeforces 500B New Year Permutation( Floyd + 贪心 )
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 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. Thej-th character of the i-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. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n.
题意:序列 a1, a2, ..., an , b1, b2, ..., bn, 如果存在整数 k (1 ≤ k ≤ n) 符合 a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 并且 ak < bk .
则称 a 序列比 b序列好。
题目给出一个序列 p ,再给出一个n*n的矩阵A , A[i][j] = 1 ,表示p中第i个元素能跟第j个元素交换 。
问p经过交换后,最好的序列是什么。
做法是用Floyd先处理任意pi , pj 能否交换 O(n^3)
然后再将序列从前到后贪心交换一次。
#include <bits/stdc++.h>
using namespace std;
const int N = ;
int n , a[N] , A[N][N];
string s ;
int main()
{
cin >> n;
for( int i = ; i <= n ; ++i ) cin >> a[i];
for( int i = ; i <= n ; ++i ) {
cin >> s ;
for( int j = ; j < n ; ++j ){
if( s[j] == '' ) A[i][j+] = ;
else A[i][j+] = ;
}
}
for( int i = ; i <= n ; ++i ){
for( int j = ; j <= n ; ++j ){
if( A[j][i] == ) continue ;
for( int k = ; k <= n ; ++k ){
if( A[i][k] ) A[j][k] = ;
}
}
} for( int i = ; i <= n ; ++i ){
for( int j = i + ; j <= n ; ++j ){
if( A[i][j] && a[i] > a[j] ) swap( a[i] , a[j] );
}
}
for( int i = ; i <= n ; ++i ) cout << a[i] << ' ';
}
Codeforces 500B New Year Permutation( Floyd + 贪心 )的更多相关文章
- Codeforces 500B. New Year Permutation[连通性]
B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- codeforces 500B.New Year Permutation 解题报告
题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...
- [CodeForces]500B New Year Permutation
刷水题做几道入门贪心题预热... 找联通块里字典序最小的放到最前面即可.记得写传递闭包 #include <iostream> #include <cstdio> #inclu ...
- Codeforces 437C The Child and Toy(贪心)
题目连接:Codeforces 437C The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- CodeForces 124C Prime Permutation (数论+贪心)
题意:给定一个字符串,问你能不能通过重排,使得任意一个素数p <= 字符串长度n,并且 任意的 i <= 长度n/素数p,满足s[p] == s[p*i]. 析:很容易能够看出来,只要是某 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心
C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...
- Educational Codeforces Round 2 C. Make Palindrome 贪心
C. Make Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...
随机推荐
- ActiveMQ的简单使用
项目中使用的介绍 一.运行ActiveMQ 在文件路径下...\apache-activemq-5.13.3\bin\win64 运行activemq.bat 这是系统中的使用,运行后还可以访问相应的 ...
- Node.js 中监听 redis key 过期事件
It is in fact possible to listen to the “expired” type keyevent notification using a subscribed clie ...
- golang指针函数
func main() { a := models.SmsVerify{} a.Id = 100 fmt.Println(a.Id) // 100 test111(a) fmt.Println(a.I ...
- 录制rtsp音视频
1.使用ffmpeg来录制rtsp视频 视频 ffmpeg -y -i rtsp://172.16.23.66:554/h264major -vcodec copy -f mp4 record.mp4 ...
- 一、Google开发者工具功能页面截图
一.利用Chrome开发者工具功能进行网页整页截图的方法. 打开你想截图的网页,然后按下 F12(macOS 是 option + command + i)调出开发者工具, 接着按「Ctrl + Sh ...
- java 指定日期后n天
RT 算时间本来就是我的弱项:不废话了,贴代码 想传什么参数自己在改改就ok,传入String,放回String public class Text { public static void main ...
- ArrayList、LinkedList、Vector区别
ArrayList.LinkedList.Vector均为可伸缩数组,即可以动态改变长度的数组. 比较ArrayList和Vector: 1. 共同点: ArrayList和Vector都是基于Obj ...
- Gitbook环境搭建及制作——2019年10月24日
1.gitbook介绍 GitBook 是一个基于 Node.js 的命令行工具,支持 Markdown 和 AsciiDoc 两种语法格式,可以输出 HTML.PDF.eBook 等格式的电子书.可 ...
- spark数据分析导论
1.spark的定义 spark是一个用来实现快速而通用的集群计算平台,高效的支持更多计算模式,包括交互式查询和流处理. 主要特点就是能够在内存中进行计算,即使在磁盘上进行计算依然比mapreduce ...
- spring-boot整合Mybatis案例(注解方式)
1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.GITHUB地址 https://github.com/nbfujx/springBo ...