10570 - Meeting with Aliens(枚举 贪心 数论)
来源:互联网 发布:如何将一个矩阵对角化 编辑:程序博客网 时间:2024/06/06 16:42
Problem D
Meeting with Aliens
Input: Standard Input
Output: Standard Output
Time Limit: 3 Seconds
The aliens are in an important meeting just before landing on the earth. All the aliens sit around a round table during the meeting. Aliens are numbered sequentially from 1 to N. It's a precondition of the meeting that i'th alien will sit between (i-1)'th and (i+1)'th alien. 1st alien will sit between 2nd and N'th alien.
Though the ordering of aliens are fixed but their positions are not fixed. In the above figure two valid sitting arrangements of eight aliens are shown. Right before the start of the meeting the aliens sometimes face a common problem of not maintaining the proper order. This occurs as no alien has a fixed position. Two maintain the proper order, two aliens can exchange their positions. The aliens want to know the minimum number of exchange operations necessary to fix the order.
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 Nindicating 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 Output for Sample Input
4
1 2 3 4
4
4 3 2 1
4
2 3 1 4
0
0
0
1
题意:给定一个序列,求最少几步得到有序序列。
思路:暴力枚举每个位置作为起点的正反序列,然后利用贪心,第i个位置放i,一个个位置去对应,最后枚举出最小的答案
代码:
#include <stdio.h>#include <string.h>const int INF = 0x3f3f3f;const int N = 505;int min(int a, int b) {return a < b ? a : b;}int n, num[N], i, j, save[N], v[N];void getstart1(int j) {for (int i = 1; i <= n; i ++) {if (j + i - 1 <= n) {save[i] = num[j + i - 1];v[num[j + i - 1]] = i;}else {save[i] = num[j + i - 1 - n];v[num[j + i - 1 - n]] = i;}}}void getstart2(int j) {for (int i = 1; i <= n; i ++) {if (j - i + 1 > 0) {save[i] = num[j - i + 1];v[num[j - i + 1]] = i;}else {save[i] = num[j - i + 1 + n];v[num[j - i + 1 + n]] = i;}}}int cal() {int ans = 0;for (int i = 1; i <= n; i ++) {if (save[i] != i) {save[v[i]] = save[i];v[save[i]] = v[i];ans ++;}}return ans;}void out() {for (int i = 1; i < n; i ++)printf("%d ", save[i]);printf("%d\n", save[i]);}int solve() {int ans = INF;for (int i = 1; i <= n ; i ++) {getstart1(i);ans = min(ans, cal());//out();getstart2(i);ans = min(ans, cal());//out();}return ans;}int main() {while (~scanf("%d", &n) && n) {for (i = 1; i <= n; i ++)scanf("%d", &num[i]);printf("%d\n", solve());}return 0;}
- 10570 - Meeting with Aliens(枚举 贪心 数论)
- UVa 10570:Meeting with Aliens(贪心)
- UVa 10570 Meeting with Aliens(思路)
- UVA 10570 Meeting with Aliens 【枚举+结论题】
- ACM 暴力枚举 Meeting with Aliens
- UVA 10570 Meeting with Aliens
- UVA 10570 Meeting with Aliens
- UVA 10570 Meeting with Aliens
- uva 10570 - Meeting with Aliens
- uva 10570 Meeting with Aliens
- UVa 10570 Meeting with Aliens
- UVA - 10570 Meeting with Aliens
- UVa - 10570 - Meeting with Aliens
- uva 10570 meeting with aliens
- UVA 10570 Meeting with Aliens
- UVA 10570 Meeting with Aliens
- UVa 10570 - Meeting with Aliens(构造法)
- UVA - 10570 Meeting with Aliens 暴力
- Flex动态组装GridColumn和获取自定义Object的属性
- android rom制作之bootimg的详细介绍和使用
- 善于经营的站长才是好站长
- T420i 简单拆机 (只拆两个螺丝清灰)
- ubuntu下设置静态ip 问题
- 10570 - Meeting with Aliens(枚举 贪心 数论)
- jQuery判断获得的对象是否存在的方法
- linux 程序包、文件 命令
- 回调函数例子
- 安装版的tomcat6.0 内存设置【tomcat6.0内存溢出】
- 位于XDB的服务器localhost要求用户名和密码
- 公有,保护,私有 派生的成员属性
- Unity3D占用内存太大的解决方法
- 如何计算阶乘运算结果尾部有多少个零