bc7 Little Pony and Permutation

本文介绍了一种将排列分解为不相交循环的算法实现,并通过一个具体的示例进行说明。该算法帮助用户找到字典序最小的解,适用于处理数学排列问题。
部署运行你感兴趣的模型镜像

As a unicorn, the ability of using magic is the distinguishing feature among other kind of pony. Being familiar with composition and decomposition is the fundamental course for a young unicorn. Twilight Sparkle is interested in the decomposition of permutations. A permutation of a set S = {1, 2, ..., n} is a bijection from S to itself. In the great magician —— Cauchy's two-line notation, one lists the elements of set S in the first row, and then for each element, writes its image under the permutation below it in the second row. For instance, a permutation of set {1, 2, 3, 4, 5} σ can be written as:
σ=(1225344351)

Here σ(1) = 2, σ(2) = 5, σ(3) = 4, σ(4) = 3, and σ(5) = 1.
Twilight Sparkle is going to decompose the permutation into some disjoint cycles. For instance, the above permutation can be rewritten as:
(1225344351)=(125)(34)

Help Twilight Sparkle find the lexicographic smallest solution. (Only considering numbers).


题目就是要求i σ(i) σ(σ(i))....选过了就不能重选,遍历一遍就OK 了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include<stack>
#include<cmath>
#include<cassert>
using namespace std;
const int N=100010;
int a[N],vis[N];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        memset(vis,0,sizeof(vis));
        int cnt=0;
        for(int i=1;i<=n;i++)
        {
            if(vis[i])
                continue;
            printf("(");
            int t=i;
            int first=1;
            while(!vis[t])
            {
                vis[t]=1;
                if(first) first=0;
                else printf(" ");
                printf("%d",t);
                t=a[t];
            }
            printf(")");
        }
        printf("\n");
    }
    return 0;
}




























































































您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

基于matlab建模FOC观测器采用龙贝格观测器+PLL进行无传感器控制(Simulink仿真实现)内容概要:本文档主要介绍基于Matlab/Simulink平台实现的多种科研仿真项目,涵盖电机控制、无人机路径规划、电力系统优化、信号处理、图像处理、故障诊断等多个领域。重点内容之一是“基于Matlab建模FOC观测器,采用龙贝格观测器+PLL进行无传感器控制”的Simulink仿真实现,该方法通过状态观测器估算电机转子位置与速度,结合锁相环(PLL)实现精确控制,适用于永磁同步电机等无位置传感器驱动场景。文档还列举了大量相关科研案例与算法实现,如卡尔曼滤波、粒子群优化、深度学习、多智能体协同等,展示了Matlab在工程仿真与算法验证中的广泛应用。; 适合人群:具备一定Matlab编程基础,从事自动化、电气工程、控制科学、机器人、电力电子等相关领域的研究生、科研人员及工程技术人员。; 使用场景及目标:①学习并掌握FOC矢量控制中无传感器控制的核心原理与实现方法;②理解龙贝格观测器与PLL在状态估计中的作用与仿真建模技巧;③借鉴文中丰富的Matlab/Simulink案例,开展科研复现、算法优化或课程设计;④应用于电机驱动系统、无人机控制、智能电网等实际工程仿真项目。; 阅读建议:建议结合Simulink模型与代码进行实践操作,重点关注观测器设计、参数整定与仿真验证流程。对于复杂算法部分,可先从基础案例入手,逐步深入原理分析与模型改进。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值