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 ...
随机推荐
- k3 cloud库存管理中的直接调拨单权限分配出现问题
k3 cloud中给直接调拨单分配了对应的权限,但是客户端无法查看到对应的单据 解决办法: 是应为没有发布到对应的客户端和浏览器端,打开bos,找到对应的单据并点击发布,找到对应的目录,如图所示: 把 ...
- 莫比乌斯反演/线性筛/积性函数/杜教筛/min25筛 学习笔记
最近重新系统地学了下这几个知识点,以前没发现他们的联系,这次总结一下. 莫比乌斯反演入门:https://blog.csdn.net/litble/article/details/72804050 线 ...
- 【LeetCode+51nod】股票低买高卖N题
[121]Best Time to Buy and Sell Stock (2018年11月25日重新复习) 给一个数组代表股票每天的价格,只能有一次交易,即一次买入一次卖出,求最大收益. 题解:用一 ...
- Vue 侦听属性
Vue 提供了一种更通用的方式来观察和响应 Vue 实例上的数据变动:侦听属性 <!DOCTYPE html> <html> <head> <meta cha ...
- mysql中文乱码解决办法
Windows 在C:\Program Files\MySQL\MySQL Server 5.5\bin目录下 MySQLInstanceConfig.exe执行 重新配置character_set_ ...
- BZOJ4710 [Jsoi2011]分特产 容斥
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4710 题解 本来想去找一个二项式反演的题的,结果被 https://www.cnblogs.c ...
- 前端每日实战:67# 视频演示如何用纯 CSS 创作单元素点阵 loader
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览.https://codepen.io/comehope/pen/YvBvBr 可交互视频 此视频是可以 ...
- BZOJ5261 Rhyme
传送门 广义后缀自动机= =+ 跟ptx大爷的博客学的 戳我传送 我写的第一种 建立Trie树的写法 bfs建立SAM 为什么是bfs呢 我也不知道(GG) 经过我一番抱大腿+询问 各位大爷说的原因是 ...
- Java类加载顺序练习题
一.题目 public class Test { public static Test t1 = new Test();// 标记1 // 普通代码块 { System.out.println(&qu ...
- 禁止打开 F12 开发者工具
禁止F12 window.onkeydown = window.onkeyup = window.onkeypress = function (event) { // 判断是否按下F12,F12键码为 ...