UVA - 10570 Meeting with Aliens(外星人聚会)(暴力枚举)
题意:输入1~n的一个排列(3<=n<=500),每次可以交换两个整数。用最少的交换次数把排列变成1~n的一个环状序列。
分析:正序反序皆可。枚举每一个起点,求最少交换次数,取最小值。
求最小交换次数solve函数,将所有不需要交换的数字用cnt统计出来,而需要交换的数字集合(个数为n)交换次数是n-1,统计一次cnt。
原因:如序列3 1 2 5 6 4,因为下标从0开始,因此将序列变为2 0 1 4 5 3,假设求以2为起点的序列正序最小交换次数,是先将2与1交换(即数字2换到位置2),再把1与0交换(即数字1换到位置1),这样交换的次数才最小。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MAXN = 500 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN << 1];
int vis[MAXN];
int solve(int a[], int n){//求最小交换次数
memset(vis, 0, sizeof vis);
int cnt = 0;
for(int i = 0; i < n; ++i){
if(!vis[i]){
++cnt;
for(int j = i; !vis[j]; j = a[j]) vis[j] = 1;
}
}
return n - cnt;
}
int main(){
int n;
while(scanf("%d", &n) == 1){
if(!n) return 0;
for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
--a[i];
a[i + n] = a[i];
}
int ans = INT_INF;
for(int j = 0; j < 2; ++j){//正序反序
for(int i = 0; i < n; ++i){//枚举起点
ans = min(ans, solve(a + i, n));
}
reverse(a, a + 2 * n);
}
printf("%d\n", ans);
}
return 0;
}
UVA - 10570 Meeting with Aliens(外星人聚会)(暴力枚举)的更多相关文章
- UVA 10570 Meeting with Aliens 外星人聚会
题意:给你一个排列,每次可以交换两个整数(不一定要相邻),求最少交换次数把排列变成一个1~n的环形排列.(正反都算) 其实就是找环了,对于一个链状序列,最小交换次数等于不在对应位置的数字个数减去环的个 ...
- UVa 10570 Meeting with Aliens (暴力)
题意:给定一个排列,每次可交换两个数,用最少的次数把它变成一个1~n的环状排列. 析:暴力题.很容易想到,把所有的情况都算一下,然后再选出次数最少的那一个,也就是说,我们把所有的可能的形成环状排列全算 ...
- UVA 10570 Meeting with Aliens
题意: N个外星人围成一桌坐下,有序的排列指N在N-1与N+1中间,现在给出一个序列,问至少交换几次可以得到有序的序列. 分析: 复制一遍输入序列,放在原序列之后.相当于环.通过枚举,可以把最小交换次 ...
- UVA - 10570 Meeting with Aliens (置换的循环节)
给出一个长度不超过500的环状排列,每次操作可以交换任意两个数,求把这个排列变成有序的环状排列所需的最小操作次数. 首先把环状排列的起点固定使其成为链状排列a,枚举排好序时的状态b(一种有2n种可能) ...
- UVA 10012 How Big Is It?(暴力枚举)
How Big Is It? Ian's going to California, and he has to pack his things, including his collection ...
- UVA 11883 Repairing a Road(最短路径+暴力枚举)
You live in a small town with R bidirectional roads connecting C crossings and you want to go from c ...
- UVA - 1262 Password(密码)(暴力枚举)
题意:给两个6行5列的字母矩阵,找出满足如下条件的“密码”:密码中的每个字母在两个矩阵的对应列中均出现.给定k(1<=k<=7777),你的任务是找出字典序第k小的密码.如果不存在,输出N ...
- 【uva 10570】Meeting with Aliens(算法效率--暴力+贪心)
题意:输入1~N的一个排列,每次可以交换2个整数,问使排列变成1~N的一个环状排列所需的虽少交换次数.(3≤N≤500) 解法:(又是一道我没打代码,光想和看就花了很久时间的题~QwQ)由于n很小,可 ...
- UVA.12716 GCD XOR (暴力枚举 数论GCD)
UVA.12716 GCD XOR (暴力枚举 数论GCD) 题意分析 题意比较简单,求[1,n]范围内的整数队a,b(a<=b)的个数,使得 gcd(a,b) = a XOR b. 前置技能 ...
随机推荐
- 小笔记----about JC
JC project JAVA,Mysql. 页面用velocity template engine render的html/css Apache Velocity template engine ...
- springboot#配置https
1.准备证书 2.1 springboot 1.x配置 2.2 springboot 2.x配置 1.准备证书: keytool -genkeypair -alias tomcat -keyalg R ...
- 洛谷 P1886 滑动窗口 /【模板】单调队列
纯板子题,入队时保证单调性,即单调栈,出队保证题目条件,本题即窗口长度k,在入队出队时都可以维护信息 ; int buf[maxm], maxq[maxm], minq[maxm], ans1[max ...
- Linux磁盘
1.磁盘的接口类型与命名方式 磁盘接口分为SATA.SCSI.SAS.PCI-E.光纤FC通道.常见的设备在Linux中的命名如下: 设备 设备在Linux内的文件名 IDE硬盘 /dev/hd[a- ...
- partialview 用法
using MvcApplication1.Models; @model MvcApplication1.Models.UserInfoModel @{ ViewBag.Title = &q ...
- windos常见软件库
1.护卫神软件库 http://soft.huweishen.com/special/1.html 2.护卫神windows资料库 http://v.huweishen.com/ 3.国超软件下载 h ...
- WinCC V7.5安装过程截图
- 从零到Django大牛的的进阶之路02
Cookie/Session Cookie Cookie以键值对的格式进行信息的存储. Cookie基于域名安全,不同域名的Cookie是不能互相访问的,如访问itcast.cn时向浏览器中写了Coo ...
- HDU 1024:Max Sum Plus Plus 经典动态规划之最大M子段和
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 一个小证明(题解 P5425 Part1)
所以这道题为什么可以这样做 嗯,我也不知道,不过我是来填坑的. \(Q\):为什么要把牛分成\(1\),\(1\)......\(N-K+1\)这样的\(K\)组呢? \(A\):我们设第\(i\)组 ...