New Year Permutation

Hermes深入理解及源码解析(二):Hermes的记忆机制 源码位置:tools/memory_tool.py存储内容为两个文件,放在 ~/.hermes/memories/:MEMORY.md:Agent 自己的笔记(环境事实、项目约定、工具坑、学到的东西)—— 上限 2200 字符USER.md:关于用户的画像(角色、偏好、沟通风格)—— 上限 1375 字符作为一个独立的工具注册在系统中(为了让智能体正确使用它,System Prompt()中明确下达了禁令存储目标存储位置数据类型持久偏好/事实MEMORY.md我是谁、我喜欢什么、环境配置。 阅读详情


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 ≤ n, i ≠ 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 ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, 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).

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.

一个数列,如果前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题就更不敢奢望了,现在感觉自己有提升,但是 作为一个初学者,想要获得提升是很容易的,还要继续走下去。


ESP32-IDF开发之旅 基础篇⑨ ESP-IDF的FreeRTOS任务间通信和同步 本文介绍了FreeRTOS任务间通信中的队列机制,重点讲解了队列的创建、发送和接收数据的API使用方法。文章提供了详细的技术说明和示例代码,包括xQueueCreate、xQueueSend、xQueueReceive等核心函数的参数说明和返回值解释。通过一个2生产者+1消费者的实际案例,展示了如何在ESP-IDF环境下使用FreeRTOS队列实现任务间通信。示例中包含了数据发送失败处理和接收数据验证等实用技巧,为开发者提供了可参考的实现模式。 阅读详情

相关推荐

Spring Boot 构建war 部署到tomcat下无法在Nacos中注册服务

因此,当使用外部容器时,如此处的 Tomcat 来部署项目,AbstractAutoServiceRegistration 就不能监听到容器启动事件了,也就不会尝试向服务注册中心注册当前这个微服务,那么注册就失败了,并且也就没有异常信息了。自定义获取获取外部容器端口的方法, 然后监听应用启动事件,当应用被启动时,获取外部容器启动的端口号,然后将这个 port 设置到 NacosAutoServiceReigistration 中。在启动类同级目录,复制下面代码即可(不需要修改,直接构建war即可)

ttyy1112的专栏 2523

B. New Year Permutation

time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output User ainta has a permutation p1, p2, ..., pn. As the New Year

无知的我 1318

华为云Stack扩容实战:从CMDB配置到计算节点增减容,一份避坑指南

本文详细解析华为云Stack扩容实战,从CMDB配置到计算节点增减容的全流程操作指南。通过典型场景案例和避坑建议,帮助企业高效管理计算资源扩容与减容,确保业务连续性。特别针对华为云Stack的弹性扩容能力提供专业指导,涵盖存储、网络及自动化工具链的配合使用。

weixin_30591551的博客 481

Good Bye 2014, problem: (B) New Year Permutation 【并查集】

题意 给你 n 个数字的序列,然后给你 n 行字符串,如果第 i 行字符串对应的第 j 列的字符是1,说明序列 i 和 j 对应的数字可以互换,求所能互换的最小字典序的序列 思路 并查集,输入每行的时候,如果对应的列是1,就把该行和该列用并查集连接起来,然后再遍历序列的每一个数字,把属于一个联通块的数字进行比较 code #include<bits/stdc++.h> #define ...

超逸の学习技术博客 1860

win 11bitlocker恢复密匙一般情况的解决方式(这里指的是你现在使用的微软账户一直没有变更过)

本来没有打算写这一篇解决帖子的,但是最近好多微博的友友都在私我询问解决方法,孩子虽然很热心,但是真的回复不过来了,打字太累了,这里给大家简单指个路,希望能够对大家有所帮助。 当时出现这个问题的时候,一搜网上的解决方法都是重装系统什么的,真是吓死孩子啦。后来去微软的官网倒腾了半天解决了问题。其实只要你没有改过微软账户,你可以直接试试我这个方法,大概三到五分钟解决问题吧。 和我情况不一样的也可以参考一下贴吧里一些大佬的分享,或者实在着急我感觉去淘宝上找个解决也不是未尝不可。 出现问题: 解决方案: 直接去微软

zhiqingxue的博客 2万+

手把手教你逆向抖店滑块验证码:从抓包到生成captchaBody的完整流程(附Python复现代码)

本文详细解析了抖店滑块验证码的逆向过程,从抓包分析到参数生成,最终用Python完整复现验证流程。涵盖关键接口分析、指纹参数生成、轨迹加密逻辑及核心加密算法实现,帮助开发者突破抖店的多重安全机制,实现高效的人机验证绕过。

weixin_30681121的博客 436

【codeforces】New Year Permutation(floyd算法进阶)

New Year Permutation Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description User ainta has a permutation p1, p2, ..., pn. As the New

better_space的博客 601

PCAN-Explorer5使用教程

在PCAN中新建工程,导入DBC文件(右键apply),导入采集的数据选择对应的CAN通道点击红框中的标记可选择想要查看的信号在plot界面跳出属性,可调整信号显示的样式和最大最小值调出cursor,选择需要观测的变量,选中右键style——Period 可显示间隔时间增加cursor,可观测多个变量。

weixin_43997043的博客 6921

CodeForces 500B New Year Permutation(序列的变换)

http://codeforces.com/problemset/problem/500/B B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard

Shyazhut 366

TIA Portal (博图) -两台不同网段S7-1200通信(上)

因为不同网段意味着它们默认不在同一个广播域,并且没有网关设备来处理路由,所以标准的 TCP/IP 通信(如 `TSEND_C`/`TRCV_C`)无法直接建立连接。S7 通信 (PUT/GET): 最简单,配置好连接后,在客户端 PLC 使用 `PUT` 指令发送数据到服务器 PLC,使用 `GET` 指令从服务器 PLC 读取数据。* 在地址详细信息中,确保 本地连接端点是客户端 PLC 的接口,远程连接端点是服务器 PLC 的接口和 IP (`192.168.2.20`)。

电工 2695

英飞凌TC3xx之一起认识GTM系列(六)如何实现GTM与VADC关联的配置

本文给出了GTM与EVADC之间关联的细节,并通过两个实例分别说明了如何对寄存器进行配置。

无所适从的我的博客 2938

CF 500B New Year Permutation

B. New Year Permutation time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output User ainta has a permutation p1, p2, …, pn. As the New Year is com

乱齐八糟 732

2026华为OD面试题029:勇攀数字高峰

在给定的数字地形图里寻找登山路径,数字代表当前位置的海拔高度。要求从最低海拔出发,不断攀登,最终到达最高山峰。地图保证最低海拔和最高山峰都只有一个。 登山规则: - 路径上的海拔必须严格递增 - 只能向上下左右 4 个方向移动 - 路径必须从最低海拔开始,到最高海拔结束 - 每个地点只能走一次 - 每一步的攀登高度差必须大于 0,且不超过指定值 `K` 输出满足条件的登山路径的数量。

琅嬛福地 136

B - New Year Permutation

#include #include #include #include using namespace std; #define MAXN 333 #define INF 1111111 struct node {     int val, id;     bool operator     {         if(val != b.va

u013570474的专栏 457

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

New Year and Permutation

C. New Year and Permutation time limit per test1 second memory limit per test1024 megabytes inputstandard input outputstandard output Recall that the permutation is an array consisting of n distinct i...

sky123博客 404

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

C. New Year and Permutation

链接:https://codeforces.ml/contest/1284/problem/C Recall that the permutation is an array consisting ofnndistinct integers from11tonnin arbitrary order. For example,[2,3,1,5,4][2,3,1,5,4]is a p...

Luoriliming的博客 328

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

【CodeForces 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,

anantheparty的博客 533

New Year Permutation(floyd)

Codeforce 500B B. New Year Permutation time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output User ainta has a permutation p 1, p 2, …, p n. As the New Year is coming, he wants to make his permutation as

Gyibin的博客 244

Codeforces 500B New Year Permutation [贪心]

B. New Year Permutation time limit per test2 seconds memory limit per test256 megabytes input standard input output standard outputUser ainta has a permutation p1, p2, …, pn. As the New Year is com

C'est la vie. 459

CF_500B New Year Permutation

B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output User ainta has a permutation p1, p2

EaShion1994的专栏 1008

B. New Year Permutation (CF)

B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output User ainta has a permutation p1, p2

叶孤心的专栏 1433
上一篇: 棋盘问题
下一篇: Catch That Cow
Techay
博客等级 码龄12年 11粉丝 174原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址