CF - 500B New Year Permutation

本文介绍了一个有趣的问题——如何通过特定的元素交换方式优化一个排列,使其变得更“漂亮”。文章提供了详细的解题思路及代码实现。
New Year Permutation
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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 1 andn 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 Input

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

Hint

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).

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.


题意:给你一组排列P的数和一个二阶矩阵A,当且仅当矩阵A的元素为1时,数列P所对应的行和列的数字可以交换。你的任务就是通过这种方式,使数列P中较小的元素尽量靠前。
解题思路:刚开始看这道题时以为简简单单的把可以调换的两个数比较一下,小的在前就行了。但这只是想想,还是没理解题意,事实上没那么简单。。(在此之前要加上个Floyd三层循环,把所有可以调换的数(包括直接,间接)都先找出来,然后再比较可以调换的数就行了。。)
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char map[310][310];
int main(){
	int n;int a[310];
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	for(int i=0;i<n;i++){
		scanf("%s",map[i]);//老套路,按字符串输入 
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			for(int k=0;k<n;k++){
				if(map[j][i]=='1'&&map[i][k]=='1'){
					map[j][k]='1';//把间接的可以调换的转换成直接的 
				}
			}	
		}
	}
//	for(int i=0;i<n;i++){
//		printf("%s\n",map[i]);
//	}
	for(int i=0;i<n;i++){
		for(int j=i+1;j<n;j++){
			if(map[i][j]=='1'&&a[i]>a[j]){//直接判断就行啦! 
				swap(a[i],a[j]);
			}
		}
	}
	for(int i=0;i<n;i++){
		printf("%d ",a[i]);
	}
	return 0;
}



打开链接下载源码: https://pan.quark.cn/s/a4b39357ea24 QT框架是由Qt公司设计的一种跨平台C++图形用户界面应用程序开发工具包,该框架被广泛地应用于桌面电脑、移动设备以及嵌入式系统等领域。QTableView作为QT框架中的一个核心组件,其主要功能是用于展示表格形式的数据,并且常常与QAbstractItemModel或QSqlTableModel等模型类协同工作。在QTableView中嵌入自定义组件,例如按钮,能够实现更加多样化的用户交互功能。 在QT框架环境下,若想在QTableView的一列中嵌入两个按钮,我们需要掌握以下几个关键的技术要点: 1. **QTableView**:QTableView是QTableView类的一个实例,它提供了一个二维的表格视图界面,可以用来展示和编辑模型中的数据。QTableView能够显示由QAbstractItemModel子类所提供的数据,例如QStandardItemModel或QAbstractTableModel等。 2. **QTableWidgetItem**:在QTableView中,QTableWidgetItem是构成表格单元格的基本对象,它用于表示表格中每一行每一列的数据。在默认情况下,QTableView仅能展示文本信息,但通过继承QTableWidgetItem并重新绘制,我们可以实现自定义的内容,比如嵌入按钮。 3. **自定义视图项**:若要在单元格内部嵌入两个按钮,我们需要开发一个自定义的QTableWidgetItem子类,该子类中包含两个QPushButton。这个子类需要重写paintEvent()方法以绘制按钮,并且实现必要的信号和槽机制来处理按...
内容概要:本文系统研究了LLC谐振变换器的变频移相混合控制模型,并基于Simulink平台进行了完整的仿真实现。文章首先阐述了LLC谐振变换器在高频高效电源转换中的工作原理与技术优势,重点提出了一种融合变频控制与移相控制的混合调控策略,旨在拓宽输出调节范围并提升系统的动态响应能力与运行效率。通过建立精确的系统数学模型,设计了复合控制框图,并在Simulink中搭建仿真系统,全面验证了该控制策略在不同负载条件和输入电压波动下的稳定性、效率表现及软开关实现能力。仿真结果表明,所提出的混合控制方法能有效降低开关损耗,提高能量转换效率,具备良好的工程应用前景。; 适合人群:具备电力电子技术、自动控制理论基础,熟悉Simulink仿真环境,从事高频电源变换器、谐振变换器设计与优化的研究生、科研人员及电力电子领域工程技术人员。; 使用场景及目标:①用于高性能LLC谐振变换器控制系统的设计与动态性能优化;②为软开关技术在电力电子变换器中的应用提供仿真验证平台;③支撑相关课题的科研论文撰写、项目开发与创新方案验证。; 阅读建议:建议读者结合Simulink仿真模型文件进行同步操作,深入理解变频与移相控制的协调机制、控制环路设计及关键参数整定方法,重点关注软开关实现条件与系统效率优化路径,以促进理论研究向实际工程应用的转化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值