uva 10570 Meeting with Aliens

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 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                                   Output for Sample Input

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

 


Problemsetter: Md. Kamruzzaman, Member of Elite Problemsetters' Panel

Special thanks to Derek Kisman

求最少交换次数,使得1~n排列有序。找出环的数目,答案就是n-环数。eg (13254)可分为(1)、(23)、(54)三个环。

#include<cstdio>
#include<map>
#include<queue>
#include<cstring>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 1000 + 5;
const int INF = 1000000000;
typedef long long LL;
typedef pair<LL, int> P;

int a[maxn];
int vis[maxn];
int n;

int solve(int l, int r){
    memset(vis, 0, sizeof  vis);
    int cnt = 0;
    for(int i = l;i <= r;i++){
        if(vis[i] == 1)
            continue;
        cnt++;
        int start = i, now = a[i]+l-1;
        while(now != start){
            vis[now] = 1;
            now = a[now]+l-1;
        }
    }
    return n-cnt;
}

int main(){
    while(scanf("%d", &n)){
        if(n == 0) break;
        for(int i = 0;i < n;i++){
            scanf("%d", &a[i]);
            a[i+n] = a[i];
        }
        int ans = INF;
        for(int i = 0;i < n;i++)
            ans = min(ans, solve(i, i+n-1));
        reverse(a, a+n);
        for(int i = 0;i < n;i++)
            a[i+n] = a[i];
        for(int i = 0;i < n;i++)
            ans = min(ans, solve(i, i+n-1));
        printf("%d\n", ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值