New Year Permutation

本文探讨如何在给定的矩阵约束下,通过特定的矩阵元素交换操作,优化数列排列,使其达到最佳顺序。重点在于理解排序规则与矩阵交互逻辑,实现数列的最佳化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



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) wherea1 = 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 ifAi, 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 where1 ≤ 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).

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.

一个数列,如果前k个数相等,a[k+1]<b[k+1]那么 a比b好,给出一个矩阵,如果aij=1,那么i和j位置的数是可以交换的,求给出矩阵状态下最好的数列。其实这个比较法就是尽量把小的数放在前面,但是加了一个交换的位置条件。自己想法就是把每个位置遍历,然后把连通分支整个找出来,再内部排序,赋值,接着找下一个连通分支,重复上述动作,循环n次,虽然有点重复,但是只有300个点,并不会超时。话说1A真开心。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int b[303][303];
int a[303], leap[303], dp[303];
queue<int>p;
int main()
{
	int i, j, m, n, ans, temp;
	char c;
	cin >> n;
	for (i = 1; i <= n; i++)
		cin >> a[i];
	getchar();
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= n; j++)
		{
			c = getchar();
			b[i][j] = c - '0';
		}
		getchar();
	}
	for (i = 1; i <= n; i++)
	{
		p.push(i);
		memset(leap, 0, sizeof(leap));
		leap[i] = 1;
		while (p.size())
		{
			temp = p.front();
			for (j = 1; j <= n; j++)
			{
				if (b[temp][j] == 1 && !leap[j])
				{
					p.push(j);
					leap[j] = 1;
				}
			}
			p.pop();
		}
		ans = 0;
		for (j = 1; j <= n; j++)
		{
			if (leap[j])
			{
				ans++;
				dp[ans] = a[j];
			}
		}
		sort(dp + 1, dp + 1 + ans);
		ans = 0;
		for (j = 1; j <= n; j++)
		{
			if (leap[j])
			{
				ans++;
				a[j] = dp[ans];
			}
		}
	}
	for (i = 1; i <= n; i++)
		cout << a[i] << " ";
	cout << endl;
	return 0;
}
这是我当时第一次做cf的b题,当时的a题还是50分钟这样做出来的,b题就更不敢奢望了,现在感觉自己有提升,但是 作为一个初学者,想要获得提升是很容易的,还要继续走下去。


%% capital coefficient matrix % I get the data of capital stock, investment, and depreciation from % EUKLEMS Capital input database, the raw data is valued in national currency %% major variables obtained in this code file: % euklems_capital_stock, euklems_investment, euklems_depreciation % all in the dimension of 1230 * 1230 * 13 cd ..\data\EUKLEMS\capital %% import data from EUKLEMS Capital Input excels % create matrices to load capital stock data capital_stock_euklems_bycountry (loading the raw data from excel) % each excel representing one country; within each excel file, each sheet representing one asset classes % in each matrix of one sheet, conlumn is for year and row is for using industry % m is a list for assets classes of 8 in euklems data, residential structure is not used, need to check how to deal with the 'other' % gross capital formation is obtained here. % the depreciation, named "consumption of fixed capital in EUKLEMS", labeled as D_ in the excel file, is also obtained here. % all three variables are in 1995 prices %% Asset class for capital stock (real), real investment (capital formation), % nominal investment, depreciation (real), and capital compensation % The code for the countries is based on the alphabetical order of the % acronom of the countries. There are 14 countries contained in the EUKLEMS database. % those excels contains viarous sheet tabs. Those in the list are just the % tab names in all of those excel files. capital_stock_sheets = ["K_IT", "K_CT", "K_Soft", "K_TraEq", "K_OMach", "K_OCon", "K_RStruc", "K_Other"]; real_investment_sheets = ["Iq_IT", "Iq_CT", "Iq_Soft", "Iq_TraEq", "Iq_OMach", "Iq_OCon", "Iq_RStruc", "Iq_Other"]; depreciation_sheets = ["D_IT", "D_CT", "D_Soft", "D_TraEq", "D_OMach", "D_OCon", "D_RStruc", "D_Other"]; nominal_investment_sheets = ["I_IT", "I_CT", "I_Soft", "I_TraEq", "I_OMach", "I_OCon", "I_RStruc", "I_Other"]; capital_compensation_sheets = ["CAP_IT", "CAP_CT", "CAP_Soft", "CAP_TraEq", "CAP_OMach", "CAP_OCon", "CAP_RStruc", "CAP_Other"]; % Country indices, 21 and 24 represent Japan and Korea which have some % different data structure country_excel_indices = [1, 2, 4, 9, 10, 11, 12, 14, 22, 23, 30, 36, 37, 40]; num_countries_euklems = length(country_excel_indices); num_assetsclass = 8; num_years = 13; % need to check where I deal with country 21 and 24 % Initialize 4-dimensional matrices to store data % Calculate the rate of return: compensation over capital stock capital_stock_bycountry=zeros(37, num_years, num_assetsclass, num_countries_euklems); real_investment_bycountry=zeros(37, num_years, num_assetsclass, num_countries_euklems); depreciation_bycountry=zeros(37, num_years, num_assetsclass, num_countries_euklems); nominal_investment_bycountry=zeros(37, num_years, num_assetsclass, num_countries_euklems); capital_compensation_bycountry=zeros(37, num_years, num_assetsclass, num_countries_euklems); % import data from excel using the function table2array for i=1:num_countries_euklems % Create the country-specific Excel file name str_country = [ '',num2str(country_excel_indices(i)),'.xls']; for w=1:num_assetsclass capital_stock_bycountry(:,:,w,i) = readmatrix(str_country,'Sheet',capital_stock_sheets(w),'Range','AB3:AN39'); % AB3:AN39 is from 1995 to 2007 for all industries real_investment_bycountry(:,:,w,i) = readmatrix(str_country,'Sheet',real_investment_sheets(w),'Range','AB3:AN39'); depreciation_bycountry(:,:,w,i)= readmatrix(str_country,'Sheet',depreciation_sheets(w),'Range','AB3:AN39'); nominal_investment_bycountry(:,:,w,i) = readmatrix(str_country,'Sheet',nominal_investment_sheets(w),'Range','AB3:AN39'); capital_compensation_bycountry(:,:,w,i) = readmatrix(str_country,'Sheet',capital_compensation_sheets(w),'Range','AB3:AN39'); end end %% replace the missing value with 0 % Replace infinite values and NaN values in the created matrices capital_stock_bycountry(isinf(capital_stock_bycountry))=0; capital_stock_bycountry(isnan(capital_stock_bycountry))=0; real_investment_bycountry(isinf(real_investment_bycountry))=0; real_investment_bycountry(isnan(real_investment_bycountry))=0; depreciation_bycountry(isinf(depreciation_bycountry))=0; depreciation_bycountry(isnan(depreciation_bycountry))=0; nominal_investment_bycountry(isinf(nominal_investment_bycountry))=0; nominal_investment_bycountry(isnan(nominal_investment_bycountry))=0; capital_compensation_bycountry(isinf(capital_compensation_bycountry))=0; capital_compensation_bycountry(isnan(capital_compensation_bycountry))=0; % now the dimension of the matrix: industry, year, assets class, country % for example, capital_stock_bycountry(:,:,1,1) gives how much IT capital stock that 37 sectors use in production over 13 years in Austria. % Check for NaN or Inf values in the matrices if any(isnan(capital_stock_bycountry(:))) || any(isinf(capital_stock_bycountry(:))) disp('capital_stock_bycountry contains NaN or Inf values'); end if any(isnan(depreciation_bycountry(:))) || any(isinf(depreciation_bycountry(:))) disp('depreciation_bycountry contains NaN or Inf values'); end if any(isnan(real_investment_bycountry(:))) || any(isinf(real_investment_bycountry(:))) disp('real_investment_bycountry contains NaN or Inf values'); end if any(isnan(nominal_investment_bycountry(:))) || any(isinf(nominal_investment_bycountry(:))) disp('real_investment_bycountry contains NaN or Inf values'); end if any(isnan(capital_compensation_bycountry(:))) || any(isinf(capital_compensation_bycountry(:))) disp('capital_compensation_bycountry contains NaN or Inf values'); end %% permutate the capital stock data to one sorted by country and then year and lastly a matrix of asset category and industry % Move the 3rd dimension (asset classes) to the 1st position. % Move the 1st dimension (using industries) to the 2nd position. % Move the 4th dimension (countries) to the 3rd position. % Move the 2nd dimension (years) to the 4th position. % after permutation, dimension: [assets classes, using-industries, using-countries, years] capital_stock_37=permute(capital_stock_bycountry,[3 1 4 2]); real_investment_37=permute(real_investment_bycountry,[3 1 4 2]); depreciation_37=permute(depreciation_bycountry,[3 1 4 2]); nominal_investment_37=permute(nominal_investment_bycountry,[3 1 4 2]); capital_compensation_37=permute(capital_compensation_bycountry,[3 1 4 2]); %% select 30 industries form 37, leaving out the already aggregated thus repeating industries % Create a mapping array for the columns to be selected col_mapping = [1:2, 4:7, 9:19, 21:24, 26:27, 29, 31:32, 34:37]; % select data for capital_stock, investment, and depreciation % selecting_industries is a function defined in a separate file in the folder called function capital_stock_30 = select_industries(capital_stock_37, col_mapping); investment_30 = select_industries(real_investment_37, col_mapping); depreciation_30 = select_industries(depreciation_37, col_mapping); nominal_investment_30 = select_industries(nominal_investment_37, col_mapping); capital_compensation_30 = select_industries(capital_compensation_37, col_mapping); % % clear the temporary variables % clear m i % clear capital_stock_bycountry capital_stock_37 % clear investment_bycountry real_investment_37 % clear depreciation_bycountry depreciation_37 %% extend the capital stock matrix to include countries that are not in the EUKLEMS data % the countries not in the EUKLEMS data are: 3, 5, 6, 7, 8, 13, 15, 16, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 29, 31, 32, 33, 34, 35, 38, 39 % the countries in the EUKLEMS data are: 1, 2, 4, 9, 10, 11, 12, 14, 22, 23, 30, 36, 37, 40 % create a new matrix to include all countries and then either EUKLEMS data OR non-EUKLEMS data in it % define constants and generate temperary matrice to store data num_all_countries = 41; n_industries = 30; capital_stock_euklems_complete_temp = zeros(num_assetsclass, n_industries, num_all_countries, num_years); investment_euklems_complete_temp = zeros(num_assetsclass, n_industries, num_all_countries, num_years); depreciation_euklems_complete_temp = zeros(num_assetsclass, n_industries, num_all_countries, num_years); nominal_investment_euklems_complete_temp = zeros(num_assetsclass, n_industries, num_all_countries, num_years); capital_compensation_euklems_complete_temp = zeros(num_assetsclass, n_industries, num_all_countries, num_years); for i = 1:num_countries_euklems country_idx = country_excel_indices(i); capital_stock_euklems_complete_temp(:, :, country_idx, :) = capital_stock_30(:, :, i, :); investment_euklems_complete_temp(:, :, country_idx, :) = investment_30(:, :, i, :); depreciation_euklems_complete_temp(:, :, country_idx, :) = depreciation_30(:, :, i, :); nominal_investment_euklems_complete_temp(:, :, country_idx, :) = nominal_investment_30(:, :, i, :); capital_compensation_euklems_complete_temp(:, :, country_idx, :) = capital_compensation_30(:, :, i, :); end % create the matrices to store the complete data which extends the asset classes to 30 num_assetsclass_all=30; capital_stock_euklems_complete = zeros(num_assetsclass_all,n_industries,num_all_countries,num_years); investment_euklems_complete = zeros(num_assetsclass_all,n_industries,num_all_countries,num_years); depreciation_euklems_complete = zeros(num_assetsclass_all,n_industries,num_all_countries,num_years); nominal_investment_euklems_complete = zeros(num_assetsclass_all,n_industries,num_all_countries,num_years); capital_compensation_euklems_complete = zeros(num_assetsclass_all,n_industries,num_all_countries,num_years); % need to confirm that communications device is also produced at % electrical, computing equipment industry % update_capital_stock is a function defined in a separate file in the folder called function, which is to allocate the 7 asset classes in EUKLEMS data to 30 asset classes vector in the complete matrices, leaving out the residential structure since it is not used in the production. This leads to the sum of each column the capital matrix not same as the total capital stock noted in the EUKLEMS data. The discrepancy is due to the residential structure. for year = 1:13 for i = 1:14 capital_stock_euklems_complete(:, :, country_excel_indices(i), year) = update_capital_stock_include_resi_structure(capital_stock_euklems_complete_temp, country_excel_indices(i), year); investment_euklems_complete(:, :, country_excel_indices(i), year) = update_capital_stock_include_resi_structure(investment_euklems_complete_temp, country_excel_indices(i), year); depreciation_euklems_complete(:, :, country_excel_indices(i), year) = update_capital_stock_include_resi_structure(depreciation_euklems_complete_temp, country_excel_indices(i), year); nominal_investment_euklems_complete(:, :, country_excel_indices(i), year) = update_capital_stock_include_resi_structure(nominal_investment_euklems_complete_temp, country_excel_indices(i), year); capital_compensation_euklems_complete(:, :, country_excel_indices(i), year) = update_capital_stock_include_resi_structure(capital_compensation_euklems_complete_temp, country_excel_indices(i), year); end end % clear capital_stock_euklems_30 depreciation_euklems_30 investment_euklems_30 % clear capital_stock_euklems_complete_temp depreciation_euklems_complete_temp investment_euklems_complete_temp %% using the capital coefficient matrix of italy and the adjusting factor (division of total capital stock intensity of the country over italy, numbered 22 % to get the capital coefficent matrix of other countries. The capital % stock total is from WIOD SEA. % adding the invesmtent and deprecation to the loop or there is other % better ways of doing this, check later cd ..\..\ wiod_k = readmatrix("k_gfcf_all.xlsx","Range",'E2:Q1441'); othercountries=[3,5,6,7,8,13,15,16,17,18,19,20,21,24,25,26,27,28,29,31,32,33,34,35,38,39]; for m=1:13 k_temp = wiod_k(:,m); for j=othercountries % Calculate the adjustment factor factor_temp=k_temp(36*(j-1)+2:36*(j-1)+35,:).'./ k_temp(36*21+2:36*21+35,:).'; factor=[factor_temp(:,1:3) sum(factor_temp(:,4:5),2)/2 factor_temp(:,6:22) sum(factor_temp(:,23:26),2)/4 factor_temp(:,27:34)]; % Update the capital coefficient matrix for other countries capital_stock_euklems_complete(:,:,j,m) =capital_stock_euklems_complete(:,:,22,m).*factor; investment_euklems_complete(:,:,j,m) = investment_euklems_complete(:,:,22,m).*factor; depreciation_euklems_complete(:,:,j,m) = depreciation_euklems_complete(:,:,22,m).*factor; capital_compensation_euklems_complete(:,:,j,m) = capital_compensation_euklems_complete(:,:,22,m).*factor; end end % until now, types of the assets is disaggregated for each using industry. %% adding the origin of each asset types in the capital coefficient matrix % Using the information of the final destination of the investment goods % in the WIOT data to . % with additional information, I convert the stock matrix complete3 into % 30*1230*13, need some some codes below to implement this %% adding the origin of each asset types in the capital coefficient matrix % Using the information of the final destination of the investment goods % in the WIOT data to . %with additional information, I convert the stock matrix complete3 into %30*1230*13, need some some codes below to implement this capital_stock_euklems_complete4=reshape(capital_stock_euklems_complete,[30,1230,13]); cd matlab\ load('wiots') year=(1995:2009); % convert each column into a 41*30 matrix capital_stock_euklems_complete_factor=zeros(30,41,1230,13); for i=1:13 wiot_nominal=WIOTs.(strcat('WIOT_',num2str(year(i)))); wiot_nominal_final = wiot_nominal(1:1435,1436:1640); % I care more about column here rather than row wiot_nominal_investment = wiot_nominal_final(:,4:5:end); %+ wiot_nominal_final(:,5:5:end) ; % now I have 41 columns for each year for 41 countries 41*1230 % above is a 1230*41 matrix, first I need to transpose this matrix to % for each country, and each asset class, I know the share of the % sourcing coutry % before, for each country, and each using industry, I know the share % of assets classes used wiot_nominal_investment_new=reshape(wiot_nominal_investment,[35,41,41]); wiot_nominal_investment_new_new= permute(wiot_nominal_investment_new,[2,1,3]); wiot_nominal_investment_new3=zeros(41,30,41); for m=1:41 wiot_nominal_investment_new_new_temp=wiot_nominal_investment_new_new(:,:,m); wiot_nominal_investment_new_new_temp2=[wiot_nominal_investment_new_new_temp(:,1:3) sum(wiot_nominal_investment_new_new_temp(:,4:5),2) wiot_nominal_investment_new_new_temp(:,6:22) sum(wiot_nominal_investment_new_new_temp(:,23:26),2) wiot_nominal_investment_new_new_temp(:,27:34)]; wiot_nominal_investment_new3(:,:,m)= wiot_nominal_investment_new_new_temp2; end wiot_nominal_investment_new_3_sum=sum(wiot_nominal_investment_new3,1); wiot_nominal_investment_new4=repelem(wiot_nominal_investment_new_3_sum,41,1,1); wiot_nominal_investment_new_ratio=wiot_nominal_investment_new3./wiot_nominal_investment_new4; wiot_nominal_investment_new5=repelem(wiot_nominal_investment_new_ratio,1,1,30); wiot_nominal_investment_new6= permute(wiot_nominal_investment_new5,[2,1,3]); capital_stock_euklems_complete_factor(:,:,:,i)=wiot_nominal_investment_new6; end capital_stock_euklems_complete_factor_new=reshape(capital_stock_euklems_complete_factor,[1230,1230,13]); capital_stock_euklems_complete5 = repelem(capital_stock_euklems_complete4,41,1,1); capital_stock_euklems_complete5_temp=reshape(capital_stock_euklems_complete5,[41,30,1230,13]); capital_stock_euklems_complete5_temp2 = permute(capital_stock_euklems_complete5_temp,[2,1,3,4]); capital_stock_euklems_complete6=reshape(capital_stock_euklems_complete5_temp2,[1230,1230,13]); euklems_capital_stock=capital_stock_euklems_complete6.*capital_stock_euklems_complete_factor_new; % here is the gross capital formation, which is essential investment % Real gross fixed capital formation, 1995 prices % following the same factor to extend the matrix for investment and % depreciation % investment investment_euklems_complete4=reshape(investment_euklems_complete,[30,1230,13]); investment_euklems_complete_factor_new=reshape(capital_stock_euklems_complete_factor,[1230,1230,13]); investment_euklems_complete5 = repelem(investment_euklems_complete4,41,1,1); investment_euklems_complete5_temp=reshape(investment_euklems_complete5,[41,30,1230,13]); investment_euklems_complete5_temp2 = permute(investment_euklems_complete5_temp,[2,1,3,4]); investment_euklems_complete6=reshape(investment_euklems_complete5_temp2,[1230,1230,13]); euklems_investment=investment_euklems_complete6.*investment_euklems_complete_factor_new; % depreciation depreciation_euklems_complete4=reshape(depreciation_euklems_complete,[30,1230,13]); depreciation_euklems_complete_factor_new=reshape(capital_stock_euklems_complete_factor,[1230,1230,13]); depreciation_euklems_complete5 = repelem(depreciation_euklems_complete4,41,1,1); depreciation_euklems_complete5_temp=reshape(depreciation_euklems_complete5,[41,30,1230,13]); depreciation_euklems_complete5_temp2 = permute(depreciation_euklems_complete5_temp,[2,1,3,4]); depreciation_euklems_complete6=reshape(depreciation_euklems_complete5_temp2,[1230,1230,13]); euklems_depreciation=depreciation_euklems_complete6.*depreciation_euklems_complete_factor_new; euklems_capital_stock(isinf(euklems_capital_stock))=0; euklems_capital_stock(isnan(euklems_capital_stock))=0; euklems_investment(isinf(euklems_investment))=0; euklems_investment(isnan(euklems_investment))=0; euklems_depreciation(isinf(euklems_depreciation))=0; euklems_depreciation(isnan(euklems_depreciation))=0; % clear i j m % clear capital_stock_euklems_complete capital_stock_euklems_complete4 capital_stock_euklems_complete5 capital_stock_euklems_complete5_temp capital_stock_euklems_complete5_temp2 capital_stock_euklems_complete6 % clear capital_stock_euklems_complete_factor capital_stock_euklems_complete_factor_new % clear col_mapping % clear depreciation_euklems_complete depreciation_euklems_complete4 depreciation_euklems_complete5 % clear depreciation_euklems_complete5_temp depreciation_euklems_complete5_temp2 depreciation_euklems_complete6 % clear depreciation_euklems_complete_factor_new factor factor_temp % clear investment_euklems_complete investment_euklems_complete4 investment_euklems_complete5 % clear investment_euklems_complete5_temp investment_euklems_complete5_temp2 investment_euklems_complete6 % clear investment_euklems_complete_factor_new k_other k_temp temp % clear wiod_k wiot_new wiot_new2 wiot_new3 wiot_nominal wiot_nominal_final % clear wiot_nominal_investment_new wiot_nominal_investment_new3 wiot_nominal_investment_new4 wiot_nominal_investment_new5 % clear wiot_nominal_investment_new6 wiot_nominal_investment_new_3_sum wiot_nominal_investment_new_new wiot_nominal_investment_new_new_temp % clear wiot_nominal_investment_new_new_temp2 wiot_nominal_investment_new_ratio wiot_tm wiot_tm_temp1 % clear wiotable cd '..\..\code'
最新发布
06-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值