【PAT甲级】1067. Sort with Swap(0,*) (25)
来源:互联网 发布:阐释者淘宝 编辑:程序博客网 时间:2024/05/21 17:02
注:第二个for循环中,始终从0开始查找会使测试用例1、2超时,因此作一个小优化,即记住上次开始查找的位置start,下次从此处开始进行查找。
#include <stdio.h>void swap(int *a, int *pos, int x, int y) { int tmp = a[x]; a[x] = a[y]; pos[a[y]] = x; a[y] = tmp; pos[tmp] = y;}int main(int argc, char *argv[]) { int n; scanf("%d", &n); int *a = new int[n]; int *pos = new int[n]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); pos[a[i]] = i; } int count = 0; bool flag = true; int start = 0; while (flag) { while (pos[0] != 0) { swap(a, pos, pos[0], pos[pos[0]]); count++; } for (int i = start; i < n; i++) { if (a[i] != i) { swap(a, pos, 0, i); start = i; count++; break; } else if (i == n - 1) { flag = false; } } } printf("%d\n", count); return 0;}
0 0
- 【PAT甲级】1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25) PAT甲级
- PAT甲级.1067. Sort with Swap(0,*) (25)
- PAT甲级练习1067. Sort with Swap(0,*) (25)
- PAT甲级1067. Sort with Swap(0,*) (25)
- PAT 甲级 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)-PAT甲级真题(贪心算法)
- pat 1067. Sort with Swap(0,*) (25)
- PAT 1067. Sort with Swap(0,*) (25)
- 【PAT】1067. Sort with Swap(0,*) (25)
- pat-1067. Sort with Swap(0,*) (25)
- 【PAT】1067. Sort with Swap(0,*)
- PAT|1067. Sort with Swap(0,*)
- PAT 1067. Sort with Swap(0,*)
- 【PAT】1067. Sort with Swap(0,*)
- pat 1067 Sort with Swap(0,*) (25)
- PAT 1025Sort with Swap(0,*) (25)
- pat-a1067. Sort with Swap(0,*) (25)
- nginx反向代理与负载均衡
- GridView基于pulltorefresh实现下拉刷新 上拉加载更多功能
- 如何将服务器上oracle11g数据dump出来导入到本地的oracle10版本
- 统计APK中方法数量
- apple mach-o linker error 报错解决
- 【PAT甲级】1067. Sort with Swap(0,*) (25)
- Android okhttp3 创建Socket的底层实现追踪
- Monocular slam 理论基础(2)
- iOS -- UITableView基本使用
- NFS服务基本配置及使用
- objc_setAssociatedObject 使用
- 算法的可视化工具
- 数据库常见时间函数的用法
- oj-9-G-计算平均分