PAT A1067. Sort with Swap(0,*) (25)

MTK Sensor驱动调试实战:从AP到SCP的完整流程解析 本文详细解析了MTK平台Sensor驱动从AP侧到SCP侧的完整调试流程。内容涵盖AP侧驱动集成、DTS配置、内核编译与验证,以及SCP侧Tinysys框架下的代码集成、Overlay配置与I2C设置等核心步骤,并分享了中断唤醒、功耗优化及传感器数据校准等实战经验与常见问题排查方法,为硬件驱动工程师提供了一份全面的实战指南。 阅读详情

题目链接

Given any permutation of the numbers {0, 1, 2,..., N-1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the swap operations in the following way:

Swap(0, 1) => {4, 1, 2, 0, 3}
Swap(0, 3) => {4, 1, 2, 3, 0}
Swap(0, 4) => {0, 1, 2, 3, 4}

Now you are asked to find the minimum number of swaps need to sort the given permutation of the first N nonnegative integers.

Input Specification:

Each input file contains one test case, which gives a positive N (<=105) followed by a permutation sequence of {0, 1, ..., N-1}. All the numbers in a line are separated by a space.

Output Specification:

For each case, simply print in a line the minimum number of swaps need to sort the given permutation.

Sample Input:
10 3 5 7 2 6 4 9 0 8 1
Sample Output:
9

AC代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100010;
int mhash[maxn];
int main() {
    int n, cnt = 0, m, count = 0;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &m);
        mhash[m] = i;
        if (m != i&&m!=0)cnt++;
    }
    int tmp,i = 1;
    while (cnt) {
        if (mhash[0] == 0) {
            //由于在本位的数我们不动,则只需要让i递增即可
            while (i < n) {
                if (mhash[i] != i) {
                    mhash[0] = mhash[i];
                    mhash[i] = 0;
                    cnt++;
                    break;
                }
                i++;
            }
        }
        tmp = mhash[0];
        mhash[0] = mhash[mhash[0]];
        mhash[tmp] = tmp;
        cnt--;
        count++;
    }
    printf("%d", count);
    return 0;
}


windows搭建IIS(FTP) 适用于参加网络系统管理的学生进行学习与参考 阅读详情

相关推荐

Ubuntu下安装Hive3.1.2教程(附MySQL安装方法及安装包)

文章目录前言一、安装Hive1. 下载压缩包并解压2. 安装Hive3. 配置环境变量4. 修改配置文件二、MySQL安装与配置1. 安装MySQL2. 安装MySQL jdbc包3. 为Hive创建MySQL账号三、验证Hive安装及错误处理1. 启动Hadoop2. 启动hive3. 运行Hive实例总结 前言 Hive 的安装需要建立在 Hadoop 的之上(类似于Hbase),关于 Hadoop 的单机安装以及伪分布式安装可以参考作者的另两篇博客: Ubuntu下单机安装Hadoop详细教程(附

程序必须为人类阅读而编写,并且仅仅碰巧可以让机器执行 1万+

1067. Sort with Swap(0,*) (25)-PAT甲级真题(贪心算法)

1067. Sort with Swap(0,*) (25) Given any permutation of the numbers {0, 1, 2,..., N-1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to us

柳婼 の blog 2883

地图坐标系大全:常用地图坐标系详解与转换指南

地图坐标系是用于描述地图上位置的数学模型。它可以用来表示地球表面上的任意一个点,使得这个点的位置可以在地图上精确定位。不同的地图坐标系采用不同的基准面和投影方式,因此会有不同的坐标系参数,不同的坐标系之间也需要进行坐标转换。在地图制图、导航、GIS等领域中,地图坐标系是一个非常重要的概念。

weixin_39888010的博客 1万+

PAT A 1067. Sort with Swap(0,*) (25)

题目 Given any permutation of the numbers {0, 1, 2,..., N-1}, it is easy to sort them in increasing order.  But what if Swap(0, *) is the ONLY operation that is allowed to use?  For example, to sort {4

xyzchenzd的专栏 713

订餐平台餐厅信息抓取:使用Python爬虫获取商家信息和用户评价

在这篇博客中,我们展示了如何使用Python爬虫抓取订餐平台上的餐厅信息和用户评价数据。通过使用requests和Selenium,我们能够有效地从静态和动态网页中提取数据,并使用pandas和matplotlib对数据进行存储和分析。未来,您可以根据需求进一步扩展功能,例如增加错误处理、增加抓取多个页面的能力,或者使用高级技术如分布式爬虫来抓取更大规模的数据。

2201_76125261的博客 1651

YOLOv11涨点改进 | 全网独家创新,损失函数改进篇 | TGRS 2025 | 引入AdaFocalLoss损失函数,改进小目标检测,自适应聚焦小目标与样本分布不平衡,提升检测精度与训练稳定性

本文提出了一种改进YOLOv11模型的AdaFocalLoss损失函数,通过动态调节机制有效提升小目标检测性能。AdaFocalLoss融合了目标尺寸感知和IoU引导的自适应调节,解决了传统损失函数在小目标检测中的样本不平衡问题。该方法自动调整训练权重,无需手动调参,显著提升了模型在多尺度目标、复杂背景下的检测精度和训练稳定性。实验表明,AdaFocalLoss能更好地平衡不同尺度目标的梯度分布,改善边界完整性,减少误检漏检。文章详细介绍了算法原理、实现代码和在YOLOv11中的集成方法,为提升目标检测性能

Ai缝合怪、助力小伙伴高效发论文 336

1067. Sort with Swap(0,*) (25)

题目如下: Given any permutation of the numbers {0, 1, 2,..., N-1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example,...

weixin_30666943的博客 182

2026年佰维存储数字IC笔试试卷带答案

建立时间是有效时钟沿到来前,输入数据必须稳定保持的最短时间。保持时间是有效时钟沿到来后,输入数据必须继续稳定保持的最短时间。

Open 104

pat 1067. Sort with Swap(0,*) (25)

这一点输入短小易于理解的模拟题,就喜欢这样的题。 方法

xunathan的专栏 811

LVGL 丝滑菜单3(Silk Menu)

h。

摸鱼专家 92

PCIE的MSI中断处理机制与优化策略

PCIe设备的。

xiaoheshang_123的博客 520

pat-a1067. Sort with Swap(0,*) (25)

简单贪心 运行时间有点长,估计是map那里,习惯了用map。。。开始两个点超时,原因在于当0在第一个时,要找一个位置不对的数交换,我每次从第一个找所以超时,应该记录上次搜寻的位置。从这个位置继续找就过了 #include #include using namespace std; int a[100010]; int main(){ map ii; int n,ans=0,pos,k=1;

dream_never_giveup的博客 314

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

PAT A1067 Sort with Swap(0,*) (25)

Given any permutation of the numbers {0, 1, 2,…, N-1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, ...

fried123123的博客 168

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

【PAT】A1067. Sort with Swap(0,*) (25)

Description:Given any permutation of the numbers {0, 1, 2,…, N-1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sor...

Duke 237

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

PAT-A-1067. Sort with Swap(0,*) (25)

1067. Sort with Swap(0,*) (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given any permutation of the numbers {

doglovecow的博客 214

PAT-A 1067. Sort with Swap(0,*) (25)

用贪心思想排序

Tomorrow Never Knows 817

PAT A1067 Sort with Swap(0,*) (25)(25 分)-----贪心法1---典型

总结: 1.算法还是很好想,如果0不在0位,就将0与该在这个位置上的数交换,如果0在0位,就把它与任意一个不在正确位置上的数交换。写的时候要注意细节,我用了一个pos[i]来表示i在start数组的位置,交换过后不仅要交换数字,还要交换位置。用了一个cnt来确定什么时候停止循环,cnt--不再正确位置上的数的个数,当其等于0的时候停止循环 2.要注意如果一个数(下标为index)和0交换过了后...

luoshiyong123的博客 346

[PAT A1067]Sort with Swap(0, i)

[PAT A1067]Sort with Swap(0, i) 题目描述 1067 Sort with Swap(0, i) (25 分)Given any permutation of the numbers {0, 1, 2,…, N−1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ...

欢迎来到我的编程小屋 603

PAT 1067. Sort with Swap(0,*) (25)

1067. Sort with Swap(0,*) (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given any permutation of the numbers {

FrozenShore 501

PAT甲级练习1067. Sort with Swap(0,*) (25)

1067. Sort with Swap(0,*) (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given any permutation of the numbers {

showerGG的博客 344

【PAT甲级 - C++题解】1067 Sort with Swap(0, i)

📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!

Newin2020的博客 796

1067 Sort with Swap(0, i)

/* Sologala @github https://github.com/Sologala/PAT_OJ PAT_oj No.1067_Sort_with_Swap */ 1067 Sort with Swap(0, i) (25 分) Given any permutation of the numbers {0, 1, 2,…, N−1}, it is easy to sort...

sologala的博客 477

PAT A1067 Sort with Swap(0, i)

PAT A1067 Sort with Swap(0, i) Sample Input: 10 3 5 7 2 6 4 9 0 8 1 Sample Output: 9 word meaning permutation sequence 排列顺序 分析: 要求只用swap(0, x),完成排序,且步数最少 刚一看,很蒙蔽,慢慢就发现了一个条件(这些数从0开始不间断),...

qq_42347617的博客 210
上一篇: PAT A1037. Magic Coupon (25)
下一篇: 用分治思想求斐波那契数列(转自追梦__的博客)
DemonInside
博客等级 码龄10年 1粉丝 18原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值