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 any permutation of the numbers {0, 1, 2,..., N-1N−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 NN nonnegative integers.
Input Specification:
Each input file contains one test case, which gives a positive NN (\le 10^5≤105) followed by a permutation sequence of {0, 1, ..., N-1N−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
/*
评测结果
时间 结果 得分 题目 编译器 用时(ms) 内存(MB) 用户
2017-07-07 11:35 答案正确 25 5-16 g++ 24 2
测试点结果
测试点 结果 得分/满分 用时(ms) 内存(MB)
测试点1 答案正确 15/15 2 1
测试点2 答案正确 3/3 24 2
测试点3 答案正确 3/3 12 1
测试点4 答案正确 2/2 2 1
测试点5 答案正确 1/1 2 1
测试点6 答案正确 1/1 2 1 有点技巧性的题目,mooc上讲了,主要是找排序的环。有0环的交换次数为n-1,无零环为n+1
建了三个数组,A是存数,B是存i号元素放在了哪个位置,Checked是放这个元素有没有被访问过。 坑:必须加一个全局变量做搜索函数起点的备忘录用,否则搜索函数反复调用会导致超时
*/
#include<stdio.h>
#define MAXN 100000
int A[MAXN],B[MAXN],Checked[MAXN];
int gFindBeginPosition=0;
int FindUnchecked(int N)
{
int i;
for(i=gFindBeginPosition;i<N;i++)
{
if(!Checked[i])
return gFindBeginPosition=i;
}
return -1;
} int main()
{
int i,N,p,sum;
int ringCount=0;
int nCount=0;
int zeroInPosition=0;
scanf("%d",&N);
for(i=0;i<N;i++)
{
scanf("%d",&A[i]);
B[A[i]]=i;
}
if(A[0]==0)
zeroInPosition=0;
else
zeroInPosition=-2; while((p=FindUnchecked(N))+1)
{
Checked[p]=1;
if(A[p]!=p)
nCount++;
else continue;
while(!Checked[B[p]])
{
p=B[p];
Checked[p]=1;
nCount++; }
ringCount++;
} sum=nCount+ringCount+zeroInPosition;
printf("%d",sum);
}
PTA 10-排序6 Sort with Swap(0, i) (25分)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- A1067 Sort with Swap(0, i) (25 分)
一.技术总结 题目要求是,只能使用0,进行交换位置,然后达到按序排列,所使用的最少交换次数 输入时,用数组记录好每个数字所在的位置. 然后使用for循环,查看i当前位置是否为该数字,核心是等待0回到自 ...
- 【PAT甲级】1067 Sort with Swap(0, i) (25 分)
题意: 输入一个正整数N(<=100000),接着输入N个正整数(0~N-1的排列).每次操作可以将0和另一个数的位置进行交换,输出最少操作次数使得排列为升序. AAAAAccepted cod ...
- PTA(Advanced Level)1067.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 ...
- PTA 1067 Sort with Swap(0, i) (贪心)
题目链接:1067 Sort with Swap(0, i) (25 分) 题意 给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次. ...
- PAT_A1067#Sort with Swap(0, i)
Source: PAT A1067 Sort with Swap(0, i) (25 分) Description: Given any permutation of the numbers {0, ...
随机推荐
- LoadRunner创建脚本和场景流程
1)脚本创建流程创建脚本->选择协议-设置录制选项-录制脚本-停止录制-优化脚本(去掉无用内容)-强化脚本(注释.代码结构调整.参数化.检查点.事物.关联)-调试脚本(观察日志) 2)场景设置的 ...
- 将服务器上的文件通过HttpWebRequest下载到本地
外网地址需要先映射. string path=""; path=@"http://222.92.71.116/P2Foundation/Images/logo.gif&q ...
- vuex的state,mutation,getter,action
开始!正常的简单的拆分下是这样的文件当然module可以在store下面新建一个文件夹用来处理单独模块的vuex管理比较合适. 1.index.js下面 import Vue from 'vue' i ...
- 轮播插件unslider.min.js使用demo
有两种应用方式: 1.轮播图片作为<img>标签使用 HTML代码: <html> <head> <meta charset="utf-8" ...
- java面试题(杨晓峰)---第八讲谈谈Vector,ArrayList,LinkedList有何区别?
Vector,ArrayList,LinkedList均为线性的数据结构,但是从现实方式与应用场景中又存在差别. 1 底层实现方式 ArrayList内部数组来实现,LinkedList内部采用双向链 ...
- 关于一些Spring MVC控制器的参数注解总结
昨天同事问我控制器参数的注解的问题,我好久没那样写过,把参数和url一起设置,不过,今天我看了一些文章,查了一些资料,我尽可能的用我自己的理解方式来解释它吧! 1.@RequestParam绑定单个请 ...
- Java中什么是匿名对象,空参构造方法输出创建了几个匿名对象,属性声明成static
package com.swift; //使用无参构造方法自动生成对象,序号不断自增 public class Person { private static int count; //如果在定义类时 ...
- iOS 解决ipv6问题
解决ipv6的方法有很多种,由于现在国内的网络运营商还在使用ipv4的网络环境,所以appstore应用不可能大范围去修改自己的服务器, 而且国内的云服务器几乎没有ipv6地址. 这里附上苹果开发平台 ...
- 记住密码功能 JS结合JQuery 操作 Cookie 实现记住密码和用户名!
// 记住密码功能 JS结合JQuery 操作 Cookie 实现记住密码和用户名! var username = document.getElementById("username&quo ...
- Python语言程序设计之一--for循环中累加变量是否要清零
最近学到了Pyhton中循环这一章.之前也断断续续学过,但都只是到了函数这一章就停下来了,写过的代码虽然保存了下来,但是当时的思路和总结都没有记录下来,很可惜.这次我开通了博客,就是要把这些珍贵的学习 ...