Problem UVA1616-Caravan Robbers

Accept: 531  Submit: 2490
Time Limit: 3000 mSec

Problem Description

Input

Input will start with a positive integer, N (3 ≤ N ≤ 500) the number of aliens. In next few lines there will be N distinct integers from 1 to N indicating the current ordering of aliens. Input is terminated by a case where N = 0. This case should not be processed. There will be not more than 100 datasets.

 Output

For each set of input print the minimum exchange operations required to fix the ordering of aliens.
 

 Sample Input

4
1 2 3 4
4
4 3 2 1
4
2 3 1 4
0
 

Sample Output

0
0
1

题解:这个题很有价值。想到倍长数列是比较自然的,但是接下来怎么办,如何快速求出将一个序列排成有序的最小交换次数,这里要用到一个结论:对于一个长度为n的元素互异的序列,通过交换实现有序的最小的交换次数是=n - n被分解成单循环的个数。具体证明见如下博客:

https://blog.csdn.net/wangxugangzy05/article/details/42454111

明白了这个,题目就变得很简单了,枚举起点,dfs找环,取最大值得出结果,这里要注意一点就是序列既可以是升序,也可以是降序,因此要倒着再枚举一遍,方法不变。

 #include <bits/stdc++.h>

 using namespace std;

 const int maxn =  + ;

 int n;
int num[maxn << ];
bool vis[maxn]; void dfs(int st, int a) {
if (vis[a]) return;
vis[a] = true;
dfs(st, num[st + a - ]);
} void dfs2(int st, int a) {
if (vis[a]) return;
vis[a] = true;
dfs2(st, num[st - a + ]);
} int main()
{
//freopen("input.txt", "r", stdin);
while (~scanf("%d", &n) && n) {
for (int i = ; i < n; i++) {
scanf("%d", &num[i]);
num[i + n] = num[i];
} int Max = ; for (int st = ; st < n; st++) {
memset(vis, false, sizeof(vis));
int cnt = ;
for (int i = st; i < st + n; i++) {
if (!vis[num[i]]) {
dfs(st, num[i]);
cnt++;
}
}
Max = Max > cnt ? Max : cnt;
} for (int st = * n - ; st >= n; st--) {
memset(vis, false, sizeof(vis));
int cnt = ;
for (int i = st; i >= st - n + ; i--) {
if (!vis[num[i]]) {
dfs2(st, num[i]);
cnt++;
}
}
Max = Max > cnt ? Max : cnt;
} printf("%d\n", n - Max);
}
return ;
}

UVA10570-Meeting with Aliens(枚举)的更多相关文章

  1. UVA-10570 Meeting with Aliens (枚举+贪心)

    题目大意:将一个1~n的环形排列变成升序的,最少需要几次操作?每次操作可以交换任意两个数字. 题目分析:枚举出1的位置.贪心策略:每次操作都保证至少一个数字交换到正确位置上. # include< ...

  2. uva10570 Meeting with Aliens

    先证明把每次i放到i位置最后次数最少:感觉,可以,用归纳法? //在序列后再加一个相同的序列,就可以模拟用各个数字开头的情况了每个位置不对的只需要换一次54123 ,5固定->41235变成12 ...

  3. UVA - 10570 Meeting with Aliens(外星人聚会)(暴力枚举)

    题意:输入1~n的一个排列(3<=n<=500),每次可以交换两个整数.用最少的交换次数把排列变成1~n的一个环状序列. 分析:正序反序皆可.枚举每一个起点,求最少交换次数,取最小值. 求 ...

  4. UVA 10570 Meeting with Aliens

    题意: N个外星人围成一桌坐下,有序的排列指N在N-1与N+1中间,现在给出一个序列,问至少交换几次可以得到有序的序列. 分析: 复制一遍输入序列,放在原序列之后.相当于环.通过枚举,可以把最小交换次 ...

  5. UVA - 10570 Meeting with Aliens (置换的循环节)

    给出一个长度不超过500的环状排列,每次操作可以交换任意两个数,求把这个排列变成有序的环状排列所需的最小操作次数. 首先把环状排列的起点固定使其成为链状排列a,枚举排好序时的状态b(一种有2n种可能) ...

  6. UVA 10570 Meeting with Aliens 外星人聚会

    题意:给你一个排列,每次可以交换两个整数(不一定要相邻),求最少交换次数把排列变成一个1~n的环形排列.(正反都算) 其实就是找环了,对于一个链状序列,最小交换次数等于不在对应位置的数字个数减去环的个 ...

  7. 【习题 8-13 UVA - 10570】Meeting with Aliens

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举1的位置在i 往右摆成一排. a[i+1]..a[n]..a[1]..a[i-1]变为有序的 ->寻找循环节,每个循环节的 ...

  8. 【uva 10570】Meeting with Aliens(算法效率--暴力+贪心)

    题意:输入1~N的一个排列,每次可以交换2个整数,问使排列变成1~N的一个环状排列所需的虽少交换次数.(3≤N≤500) 解法:(又是一道我没打代码,光想和看就花了很久时间的题~QwQ)由于n很小,可 ...

  9. UVa 10570 Meeting with Aliens (暴力)

    题意:给定一个排列,每次可交换两个数,用最少的次数把它变成一个1~n的环状排列. 析:暴力题.很容易想到,把所有的情况都算一下,然后再选出次数最少的那一个,也就是说,我们把所有的可能的形成环状排列全算 ...

  10. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

随机推荐

  1. python面向对象学习(五)多态

    多态 多态:不同的 子类对象 调用相同的 父类方法,产生不同的执行结果 多态 可以 增加代码的灵活度 以 继承 和 重写父类方法 为前提 是调用方法的技巧,不会影响到类的内部设计 多态案例练习 需求 ...

  2. Async/Await是这样简化JavaScript代码的

    译者按: 在Async/Await替代Promise的6个理由中,我们比较了两种不同的异步编程方法:Async/Await和Promise,这篇博客将通过示例代码介绍Async/Await是如何简化J ...

  3. java框架之spring

    一.HelloWorld程序 导入四个核心包(core.beans.expression.context)和一个logging的包: 写一个类并在 xml 中配置相应的bean(两个重要属性 id 和 ...

  4. JavaScript之Object对象常用属性与方法手册

    MDN Object参考地址:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Obje ...

  5. 洛谷P2866 [USACO06NOV]糟糕的一天Bad Hair Day(单调栈)

    题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self ...

  6. Duplicate entry '0' for key 'PRIMARY'

    一般使用ORM时,提交新增实体时, mysql会出现此错误:Duplicate entry '0' for key 'PRIMARY' 原因是插入语句,未提供主键的值,且主键是非自增长的. 解决办法是 ...

  7. iOS----------The app's Info.plist must contain an NSPhotoLibraryUsageDescription key

    This app has crashed because it attempted to access privacy-sensitive data without a usage descripti ...

  8. 关于苹果延迟了App接入HTTPS服务截止日期

    可参考 http://www.cocoachina.com/apple/20161223/18431.html https://developer.apple.com/news/?id=1221201 ...

  9. 小程序问题集:保存失败:Error: ENOENT: no such file or directory, open

    问题如图: 当编译的时候 会提示找不到这个文件(index),但是确信项目目录里已经删除了该页面路径,并且app.json的pages列表中也没有该页面:   这时候需要看一下当前已经打开的文件中是否 ...

  10. java 返回某一天的周日和现在这一周的周日

    import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import j ...