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 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

问题链接:UVA10570 Meeting with Aliens
问题简述:(略)
问题分析:数学计算问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10570 Meeting with Aliens */#include <bits/stdc++.h>using namespace std;const int INF = 0x3f3f3f3f;
const int N = 500 + 1;
int n, a[N + N], vis[N];int getans(int t[])
{memset(vis, 0, sizeof vis);int cnt = 0;for (int i = 1; i <= n; i++)if (!vis[i]) {cnt++;for (int j = i; !vis[j]; j = t[j])vis[j] = 1;}return n - cnt;
}int solve()
{int ans = INF;for (int i = 1; i <= n; i++)ans = min(ans, getans(a + i));int l = 1, r = n;while (l < r) swap(a[l++], a[r--]);for (int i = 1; i <= n; i++)a[n + i] = a[i];for (int i = 1; i <= n; i++)ans = min(ans, getans(a + i));return  ans;
}int main()
{while (~scanf("%d", &n) && n) {for (int i = 1; i <= n; i++) {scanf("%d", &a[i]);a[n + i] = a[i];}printf("%d\n", solve());}return 0;
}

UVA10570 Meeting with Aliens【数学计算】相关推荐

  1. js函数语法:ASCII 码的相互转换,字符串操作,数学计算

    ASCII 码的相互转换 for (let i = 'a'.charCodeAt(); i <= 'z'.charCodeAt(); i++) {a.push(String.fromCharCo ...

  2. P4588 [TJOI2018]数学计算(线段树维护区间乘和单点修改)

    P4588 [TJOI2018]数学计算 刚看到这题根本每想到用线段树,直接每次记录计算结果然后找到要除的数字就好了呗 但是!你会注意到,如果连续乘很多很多次,然后再除的话,如果不取模会爆 long ...

  3. MATLAB数学计算与工程分析范例教程,MATLAB数学计算与工程分析范例教程

    基本信息 书名:MATLAB数学计算与工程分析范例教程 定价:28.00元 作者:石博强,赵金 编著 出版社:中国铁道出版社 出版日期:2005-05-01 ISBN:9787#113057596 字 ...

  4. Shell脚本笔记(三)shell中的数学计算

    shell中的数学计算 一.使用方括号 #!/bin/bash a=10 b=29 c=88res=$[$a * ($c-$b)] echo $res 二.使用(()) echo $((1+9)) ( ...

  5. python数值运算答案_笨方法学Python 习题3:数字和数学计算

    数字和数学计算 print("I will now count my chickens") print("Hens",25+30/6) print(" ...

  6. 用计算机计算的手抄报内容,关于数学计算手抄报

    开办手抄报对学生是一项综合性很强的实践活动.下面是学习啦小编为大家带来的关于数学计算手抄报,希望大家喜欢. 数学计算手抄报的图片欣赏 数学计算手抄报图一 数学计算手抄报图二 数学计算手抄报图三 数学计 ...

  7. php+数学计算公式,PHP数学计算函数总结

    PHP数学计算函数总结 发布于 2015-01-21 16:53:26 | 189 次阅读 | 评论: 0 | 来源: 网友投递 PHP开源脚本语言PHP(外文名: Hypertext Preproc ...

  8. 《“笨办法”学Python(第3版)》——习题3 数字和数学计算

    本节书摘来自异步社区<"笨办法"学Python(第3版)>一书中的习题3,作者[美]Zed A. Shaw,王巍巍 译,更多章节内容可以访问云栖社区"异步社区 ...

  9. R开发(part2)--R语言中的数学计算

    学习笔记,仅供参考,有错必纠 参考自:<R的极客理想>-- 张丹 文章目录 R开发 R语言中的数学计算 对数 加权平均.连乘.差分.秩.任意数.全体数 阶乘.组合.排列 累加.累乘.最小累 ...

最新文章

  1. python解释器运行代码-python解释器怎么运行
  2. GOF23种设计模式(Design Pattern)总结
  3. Matlab---寻找峰值函数
  4. Ubuntu平台 Qt 5.x 安装方法
  5. 雷军:小米将扩招5000名工程师!多个领域需要用到C++
  6. 操作系统识别-python、nmap
  7. ffmpeg 推流同时录像命令_ffmpeg推流命令
  8. WordPress的.htaccess十个技巧
  9. 浅谈计算机教学论文,毕业论文--浅谈计算机教学的有效性
  10. UVA11021 Tribbles
  11. Java核心技术(基础知识一)
  12. CSS实现div圆角
  13. 微信公众号支付开发(java)实例详解
  14. 怎样在苹果Mac鼠标或触控板上复制和粘贴?
  15. Scrapy框架爬取新闻!
  16. PTA 7-94 奇偶数判断
  17. gitlab 企业级私有仓库搭建
  18. 【Java】JVM内存回收
  19. 如何改进企业旧式工时管理系统?
  20. 数据库系统概论 第四章课后习题(部分)

热门文章

  1. java跟setattribute,java 中的request.setAttribute和session.setAttribute的区别
  2. python连接sql引用的第三方库_python连接sqlserver数据库操作
  3. python123第四周作业_科学计算和可视化
  4. nftables-howto-zh中文手册(不完整)
  5. GDAL库读取Envisat ASAR数据
  6. (转)Managed DirectX +C# 开发(入门篇)(八)
  7. windows制作docker镜像_.NET Core程序跑在任何有docker的地方
  8. android studio 运行找不到类,android-studio – Android Studio找不到TestCase类
  9. 记录大三第一次面试经历
  10. Spark on YARN 两种模式yarn-cluster yarn-client 面试题