UVA10570 Meeting with Aliens数学计算

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA10570 Meeting with Aliens数学计算相关的知识,希望对你有一定的参考价值。

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数学计算的主要内容,如果未能解决你的问题,请参考以下文章

UVa10570 Meeting with Aliens (枚举)

UVa 10570 - Meeting with Aliens

UVa - 10570 - Meeting with Aliens

uva10570 Meeting with Aliens

UVa 10570 Meeting with Aliens (暴力)

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