PAT 1067 Sort with Swap(0,*)
来源:互联网 发布:网上模拟钢琴软件 编辑:程序博客网 时间:2024/06/08 13:20
这道题调试了好久,代码写的比较丑,总的来说,抓住1. 0是否在0的位置。2 . 一共有几个环。如果0在环内,则需要的交换次数为环中除0外的个数,如果0不在环内,则需要一个交换将0放入。
#include <stdio.h>#include <string.h>#include <iostream>using namespace std;int n,right1,count;int Father[100005];int FindFather(int x){ if (Father[x]!=-1) { Father[x]=FindFather(Father[x]); return Father[x]; } else return x;}void Union(int a,int b){ int tempa=FindFather(a); int tempb=FindFather(b); if (tempa!=tempb) { Father[tempa]=tempb; }}int main(){ int index,ringnum=0; memset(Father,0xff,sizeof(Father)); //freopen("/Users/pantingting/Documents/code/data/input", "r", stdin); scanf("%d",&n); for (int i=0; i<n; i++) { scanf("%d",&index); if (index==i) { Father[i]=-2; right1++; } else Union(index, i); } for (int i=0; i<n; i++) { if (Father[i]==-1) { ringnum++; } } int ans=n-right1+ringnum; if (Father[0]!=-2) { ans-=2; } printf("%d\n",ans); return 0;}
0 0
- PAT 1067 Sort with Swap(0,*)
- pat 1067 Sort with Swap(0,*) (25)
- 浙大PAT 1067题 1067. Sort with Swap(0,*)
- Pat(Advanced Level)Practice--1067(Sort with Swap(0,*))
- PAT (Advanced Level) Practise 1067 Sort with Swap(0,*) (25)
- PAT (Advanced Level) Practise 1067 Sort with Swap(0,*) (25)
- 【PAT】1067. Sort with Swap(0,*)
- pat 1067. Sort with Swap(0,*) (25)
- PAT 1067. Sort with Swap(0,*) (25)
- 【PAT】1067. Sort with Swap(0,*) (25)
- 浙大PAT-Sort with Swap(0, i)
- pat--Sort with Swap(0, i)
- PAT 1025Sort with Swap(0,*) (25)
- PAT|1067. Sort with Swap(0,*)
- pat-1067. Sort with Swap(0,*) (25)
- PAT 1067. Sort with Swap(0,*)
- pat-a1067. Sort with Swap(0,*) (25)
- 【PAT】1067. Sort with Swap(0,*)
- jvm Shallow heap & Retained heap
- win下memcache安装
- 前三位必须是大写字母的正则,长度限制5位
- 这7个开源技术,支撑起整个互联网时代
- 判断是否为平板
- PAT 1067 Sort with Swap(0,*)
- CRISP-DM (cross-industry standard process for data mining) 跨行业数据挖掘标准流程
- 远程推送机制
- Linux程序设计(Linux shell编程六)
- hdu 1269 迷宫城堡(强连通 tarjan )
- 使用脚本删除ios工程中未使用图片
- IOC的一种简易实现(四)
- swift UI专项训练28 DatePicker日期老虎机
- UVa1595 - Symmetry