PAT (Advanced Level) 1067. Sort with Swap(0,*) (25)
只对没有归位的数进行交换。
分两种情况:
如果0在最前面,那么随便拿一个没有归位的数和0交换位置。
如果0不在最前面,那么必然可以归位一个数字,将那个数字归位。
这样模拟一下即可。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std; int n;
int a[+];
queue<int>Q;
int pos[+]; int main()
{
scanf("%d",&n);
for(int i=;i<n;i++) scanf("%d",&a[i]);
for(int i=;i<n;i++) pos[a[i]]=i;
for(int i=;i<n;i++)
{
if(a[i]==) continue;
if(a[i]!=i) Q.push(a[i]);
} int ans=;
while()
{
if(a[]==)
{
while(!Q.empty())
{
int head=Q.front(); Q.pop();
if(a[head]==head) continue;
else
{
ans++;
int pos1=pos[];
int pos2=pos[head];
swap(a[pos1],a[pos2]);
swap(pos[head],pos[]);
break;
}
}
if(Q.empty()) break;
}
else
{
ans++;
int pos1=pos[];
int pos2=pos[pos[]];
int num1=;
int num2=pos[];
swap(a[pos1],a[pos2]);
swap(pos[num1],pos[num2]);
}
}
printf("%d\n",ans);
return ;
}
PAT (Advanced Level) 1067. Sort with Swap(0,*) (25)的更多相关文章
- 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 ...
- 1067. Sort with Swap(0,*) (25)【贪心】——PAT (Advanced Level) Practise
题目信息 1067. Sort with Swap(0,*) (25) 时间限制150 ms 内存限制65536 kB 代码长度限制16000 B Given any permutation of t ...
- 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 ...
- 1067. Sort with Swap(0,*) (25)
时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given any permutation of the num ...
- 【PAT甲级】1067 Sort with Swap(0, i) (25 分)
题意: 输入一个正整数N(<=100000),接着输入N个正整数(0~N-1的排列).每次操作可以将0和另一个数的位置进行交换,输出最少操作次数使得排列为升序. AAAAAccepted cod ...
- PAT甲题题解-1067. Sort with Swap(0,*) (25)-贪心算法
贪心算法 次数最少的方法,即:1.每次都将0与应该放置在0位置的数字交换即可.2.如果0处在自己位置上,那么随便与一个不处在自己位置上的数交换,重复上一步即可.拿样例举例: 0 1 2 3 4 5 ...
- PAT 1067. Sort with Swap(0,*)
1067. Sort with Swap(0,*) (25) Given any permutation of the numbers {0, 1, 2,..., N-1}, it is easy ...
- pat1067. Sort with Swap(0,*) (25)
1067. Sort with Swap(0,*) (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...
- 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 ...
随机推荐
- <context:annotation-config/>
转自:Spring <context:annotation-config/> 解说 在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-c ...
- Ubuntu基本命令--apt, dpkg
一.Ubuntu中软件安装方法 1.APT方式 (1)普通安装:apt-get install softname1 softname2 …; (2)修复安装:apt-get -f install so ...
- Cv图像处理
http://wiki.opencv.org.cn/index.php/Cv%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86 看看知识点,虽然是C 版本.
- 【Machine Learning in Action --2】K-近邻算法改进约会网站的配对效果
摘自:<机器学习实战>,用python编写的(需要matplotlib和numpy库) 海伦一直使用在线约会网站寻找合适自己的约会对象.尽管约会网站会推荐不同的人选,但她没有从中找到喜欢的 ...
- malloc without free, what happens?
It's per-process. Once your process exits, the allocated memory is returned to the OS for use by oth ...
- NSArray的containsObject漏洞
1.NSArray中的containsObject的用法 NSMutableArray *array=[NSMutableArray array]; if(![array containsObject ...
- spice-vdagent
The spice-vdagent should be running in the guest. Have you installed the spice guest tools in your w ...
- windows ORA-12560: TNS: 协议适配器错误
1.first it report ORA-12560: TNS: 协议适配器错误 手工设定环境变量如下: set ORACLE_HOME=d:\app\OAadmin\product\11.2.0\ ...
- 转 由一次磁盘告警引发的血案:du 和 ls 的区别
如果你完全不明白或者完全明白图片含义, 那么你不用继续往下看了. 否则, 这篇文章也许正是你需要的. 背景 确切地说,不是收到的自动告警短信或者邮件告诉我某机器上的磁盘满了,而是某同学人肉发现该机器写 ...
- 关于web.xml的格式
先是filter 再是<filter-mapping> 然后<servlet> 再是<servlet-mapping> 这是一种规范基于j2ee 在开发的过程中一 ...