当前位置:网站首页>1067 Sort with Swap(0, i)
1067 Sort with Swap(0, i)
2022-08-03 22:58:00 【Brosto_Cloud】
Given any permutation of the numbers {0, 1, 2,..., N−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 N nonnegative integers.
Input Specification:
Each input file contains one test case, which gives a positive N (≤105) followed by a permutation sequence of {0, 1, ..., N−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
代码:
#include <iostream>
#include <algorithm>
using namespace std;
int n, a[100010], b[100010], ans, t, x, y, ind[100010], cnt, fIndex;
bool flag;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
ind[a[i]] = i;
}
sort(b, b + n);
for (int i = 1; i < n; i++) {
if (a[i] != b[i]) {
cnt++;//记录位置不对的个数
}
}
while (true) {
while (a[0] != 0) {
t = ind[0]; //t是0的下标
x = b[ind[0]]; //x是应该在此时0的位置的数
y = ind[x]; //y是 应该在此时0的位置的数 的下标
a[y] = 0;
a[t] = x;
ind[0] = y;
ind[x] = t;
ans++;
cnt--;
}
flag = 1;
if (cnt == 0) {//顺序不对的个数归零时排序已完成
printf("%d", ans);
break;
}
//此时a[0]=0,但是不一定完成移动,选择第一个顺序不对的数与0交换
for (int i = fIndex; i < n; i++) {//fIndex记录第一个顺序不对的数的下标,及时更新
if (a[i] != b[i]) {
ind[0] = i;
ind[a[i]] = 0;
a[0] = a[i];
a[i] = 0;
ans++;
flag = 0;
fIndex = i;
break;
}
}
if (flag) {
printf("%d", ans);
break;
}
}
return 0;
}
测试点12超时 参考博客:[PAT]1067 Sort with Swap(0, i) (25 分)(运行超时解决办法)_刘好念的博客-CSDN博客
边栏推荐
- override learning (parent and child)
- 牛客2022 暑期多校3 H Hacker(SAM + 线段树查询区间内部最大子段和)
- Click the icon in Canvas App to generate PDF and save it to Dataverse
- Adobe是什么?
- BMN: Boundary-Matching Network for Temporal Action Proposal Generation Reading Notes
- 目标检测技术研究现状及发展趋势
- 物联网新零售模式,引领购物新潮流
- 目标检测的国内外研究现状
- What is the difference between the generator version and the viewer version?
- First domestic open source framework 】 【 general cloud computing framework, any program can be made into cloud computing.
猜你喜欢
Republish the lab report
亿流量大考(2):开发一套高容错分布式系统
七夕活动浪漫上线,别让网络拖慢和小姐姐的开黑时间
Summary bug 】 【 Elipse garbled solution project code in Chinese!
HCIP BGP lab report
Gains double award | know micro easily won the "2021 China digital twin solution suppliers in excellence" "made in China's smart excellent recommended products" double award!
The salary of soft testers at each stage, come to Kangkang, how much can you get?
Zilliz 2023 秋季校园招聘正式启动!
MiniAPI of .NET6 (14): Cross-domain CORS (Part 1)
noip preliminary round
随机推荐
ML之interpret:基于titanic泰坦尼克是否获救二分类预测数据集利用interpret实现EBC模型可解释性之全局解释/局部解释案例
易观分析:2022年Q2中国网络零售B2C市场交易规模达23444.7亿元
亿流量大考(2):开发一套高容错分布式系统
Scala basics [regular expressions, framework development principles]
【论文阅读】TRO 2021: Fail-Safe Motion Planning for Online Verification of Autonomous Vehicles Using Conve
Conditional Statements for Shell Programming
Research status of target detection at home and abroad
Zilliz 2023 秋季校园招聘正式启动!
Summary bug 】 【 Elipse garbled solution project code in Chinese!
Recognized by International Authorities | Yunzhuang Technology was selected in "RPA Global Market Pattern Report, Q3 2022"
P1996 约瑟夫问题
直播预告 | 构建业务智联,快速拥抱财务数字化转型
Deep integration of OPC UA and IEC61499 (1)
PowerMockup 4.3.4::::Crack
SPOJ 2774 Longest Common Substring(两串求公共子串 SAM)
IELTS essay writing template
HCIP BGP实验报告
关于IDO预售系统开发技术讲解丨浅谈IDO预售合约系统开发原理分析
[MySQL Advanced] Creation and Management of Databases and Tables
CAS:178744-28-0,mPEG-DSPE,DSPE-mPEG,甲氧基-聚乙二醇-磷脂酰乙醇胺供应