题意:

输入一个正整数N(<=100000),接着输入N个正整数(0~N-1的排列)。每次操作可以将0和另一个数的位置进行交换,输出最少操作次数使得排列为升序。

AAAAAccepted code:

 #define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int a[],b[];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
int ans=n-;
for(int i=;i<n;++i){
cin>>a[i];
b[a[i]]=i;
if(a[i]==i&&a[i])
--ans;
}
int sum=;
int pos=;
while(ans)
if(b[]!=){
swap(b[],b[b[]]);
++sum;
--ans;
}
else
for(int i=pos;i<n;++i)
if(b[i]!=i){
swap(b[],b[i]);
++sum;
pos=i+;
break;
}
cout<<sum;
return ;
}

【PAT甲级】1067 Sort with Swap(0, i) (25 分)的更多相关文章

  1. PAT 甲级 1067 Sort with Swap(0, i) (25 分)(贪心,思维题)*

    1067 Sort with Swap(0, i) (25 分)   Given any permutation of the numbers {0, 1, 2,..., N−1}, it is ea ...

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

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

  4. PTA 10-排序6 Sort with Swap(0, i) (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/678 5-16 Sort with Swap(0, i)   (25分) Given a ...

  5. PAT甲级——A1067 Sort with Swap(0, i)

    Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order ...

  6. PAT Advanced 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 orde ...

  7. 10-排序6 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 ...

  8. A1067 Sort with Swap(0, i) (25 分)

    一.技术总结 题目要求是,只能使用0,进行交换位置,然后达到按序排列,所使用的最少交换次数 输入时,用数组记录好每个数字所在的位置. 然后使用for循环,查看i当前位置是否为该数字,核心是等待0回到自 ...

  9. PTA 1067 Sort with Swap(0, i) (贪心)

    题目链接:1067 Sort with Swap(0, i) (25 分) 题意 给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次. ...

随机推荐

  1. 【vue】 router.beforeEach

    import store from '@/store' const Vue = require('vue') const Router = require('vue-router') Vue.use( ...

  2. 【转载】Java中的容器讲解

    转自:http://blog.csdn.net/garfielder007/article/details/52143794 Set,List,Map,Vector,ArrayList的区别 Java ...

  3. 工具 - deepin vscode中的oh-my-zsh乱码

    解决办法 https://blog.zhaytam.com/2019/04/19/powerline-and-zshs-agnoster-theme-in-vs-code/ git clone htt ...

  4. pytorch数学运算与统计属性入门(非常易懂)

    pytorch数学运算与统计属性入门1.Broadcasting (维度)自动扩展,具有以下两个重要特征:(1)expand (2)without copying data重点的核心实现功能是:(1) ...

  5. 一篇文章了解JsBridge

    链接:https://blog.csdn.net/duwen90/article/details/79389545

  6. 【原】linux增加开机自启动脚本

    在rc.local脚本中添加开机自启动程序

  7. Python 多任务(进程) day1(2)

    进程和线程的简单区别: 功能:进程:能够完成多任务,比如 在一台电脑上能够运行多个QQ一份资源有一个执行的剪头,有多份资源就可以执行多个语句线程:能够完成多任务,比如 在一个QQ中的多个聊天窗口一份资 ...

  8. matplotlib添加坐标轴实现性格测试可视化

    一张图展示最终效果 皮一下很开心,标签名肯定不是用户想要的,所以在这里尝试了定制化功能 功能&方法: 定制化标签名/标签数量(4个/5个).实现方法:在第一个界面点击cancel按钮,进入修改 ...

  9. Java - Test - TestNG: Idea 添加 TestNG 依赖

    1. 概述 Idea 开发的 maven 添加 testng 依赖 2. 背景 最近复习 TestNG 尝试搭建环境 发现教材和网上很多的教程, 都是 eclipse 的 我用的是 idea 这个貌似 ...

  10. VIM - ex 命令行的窗口切换

    1. 概述 操作 vim 同时编辑多个文件 约定 ctrl 使用按键 ctrl + w 时, 写作 ^W 思路 我记得这个由两个机制 ex 命令与缓冲区 分屏 2. 场景 文件 file1 file2 ...