uva 10570 meeting with aliens

//
//  main.cpp
//  uva 10570 - Meeting with Aliens
//
//  Created by XD on 15/8/17.
//  Copyright (c) 2015年 XD. All rights reserved.
//
/*
 这题我也是醉了,看错题了,一位只能相邻的可以交换,害的我好久没想出来。
 现在时任意两个都可以交换,那么就好做了。
 只要暴力枚举1的位置和形成环的方向,就可以完成了。
 */

#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<vector>
#include <string.h>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cstdio>
using namespace std ;
const int maxn = 500 + 10 ;
int  pos[maxn] ;
int t_pos[maxn] ;
int t_aliens[maxn] ;
int aliens[maxn] ;
int n ;
int min(int x , int y)
{
    return  x > y ? y:x  ;
}

void  Myswap(int x,int destination)
{
    int temp = t_aliens[destination]  ;
    t_aliens[destination] = x ;
    t_aliens[t_pos[x]] = temp ;
    t_pos[temp] = t_pos[x] ;
    t_pos[x] = destination ;
    
}
 int getNextPos(int start , int i ,int way)
{
    if (way == 0) {
        return (start + i - 1)%n ;
    }
    else{
        
        int t = (start - i +1)% n ;
        if (t <  0) {
            return t + n ;
        }
        else{
            return t ;
        }
    }
    
}
int solved(int start)
{
    int ans =1010 ;
    for (int way = 0; way < 2; way++) {
        memcpy(t_pos, pos, sizeof(int) * n + 5) ;
        memcpy(t_aliens, aliens, sizeof(int )* n + 5) ;
        int tans = 0 ;
        for (int i = 1; i <= n ; i++) {
            int destination = getNextPos(start  ,i,way) ;
            if (t_pos[i] == destination) {
                continue ;
            }
            else{
                Myswap(i, destination) ;
                tans++ ;
            }
        }
        ans =min(ans ,tans) ;
    }
    return ans  ;
}
int main() {
    while (scanf("%d" ,&n) , n) {
        for (int i = 0; i < n; i++) {
            scanf("%d" ,&aliens[i]) ;
            pos[aliens[i]] = i ;
        }
        //枚举起点
        int  ans = 1010 ;
        for (int i  = 0; i < n ; i++) {
            
            ans = min(solved(i) , ans) ;
            if (ans == 0 ) {
                break ;
            }
        }
        printf("%d\n" ,ans) ;
        
    }
    
    
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值