1067. Sort with Swap(0,*) (25)
来源:互联网 发布:mac qq远程桌面 编辑:程序博客网 时间:2024/06/13 15:25
当0在0位置时,如果其他的还未完全就位需要浪费一次交换机会。
#include <iostream>#include <cstdio>#include <vector>#include <algorithm>using namespace std;int findPos(vector<int>& per, int st){ for(size_t i = st; i < per.size(); ++i){ if(per[i] != i) return i; } return 0;}int main(){int n;cin >> n;vector<int> per(n);for(int i = 0; i < n; ++i){scanf("%d", &per[i]);}int sum = 0, pos = findPos(per, 1);while(pos){ if(per[0] == 0){ swap(per[0], per[pos]); ++sum; } while(per[0] != 0){ int tmp = per[0]; per[0] = per[tmp]; per[tmp] = tmp; ++sum; } pos = findPos(per, pos);}cout << sum;return 0;}
0 0
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- pat 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- PAT 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 【PAT】1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- apache AH00558 错误的解决
- H264编码原理以及I帧B帧P帧
- H264多参考帧个数考虑
- 2016-2小笔记
- 如何清除远程桌面连接的IP域名信息
- 1067. Sort with Swap(0,*) (25)
- c++ virtual
- IP电话知识点与协议
- 视频播放器
- Java Sax解析xml
- 开机出现指令“Ox00000000”的内存,该内容不能为read解决方法
- drop删除数据库表
- 【leetcode】【7】Reverse Integer
- 设置沉浸式状态栏