//
// 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;
}
uva 10570 meeting with aliens
最新推荐文章于 2020-04-26 19:42:56 发布