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 toN 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
|
#include <cstdio>
#include <algorithm>
using namespace std;
// array[i]代表i位置的整数
int array[510];
// place[i]代表整数i的下标
int place[510];
int n;
int tmp_place[510];
int tmp_array[510];
int main()
{
while(scanf("%d", &n) == 1 && n != 0)
{
// 读入整数
for(int i = 0; i < n; i++)
{
scanf("%d", &array[i]);
place[array[i]] = i;
}
int min_count = n+10;
// 枚举所有可能的排列情况
for(int x = 1; x <= n; x++)
{
// 枚举升序的情况
// x,x+1,...,n,1,...,x-1
int count1 = 0;
for(int i = 1; i <= n; i++)
tmp_place[i] = place[i];
for(int i = 0; i < n; i++)
tmp_array[i] = array[i];
int num = x;
for(int i = 0; i < n; i++)
{
if(tmp_array[i] != num)
{
tmp_array[tmp_place[num]] = tmp_array[i];
tmp_place[tmp_array[i]] = tmp_place[num];
tmp_place[num] = i;
tmp_array[i] = num;
count1++;
}
num++;
if(num > n)
num = 1;
}
if(min_count > count1)
min_count = count1;
// 枚举降序的情况
// x,x-1,...,1,n,...,x+1
count1 = 0;
for(int i = 1; i <= n; i++)
tmp_place[i] = place[i];
for(int i = 0; i < n; i++)
tmp_array[i] = array[i];
num = x;
for(int i = 0; i < n; i++)
{
if(tmp_array[i] != num)
{
tmp_array[tmp_place[num]] = tmp_array[i];
tmp_place[tmp_array[i]] = tmp_place[num];
tmp_place[num] = i;
tmp_array[i] = num;
count1++;
}
num--;
if(num < 1)
num = n;
}
if(min_count > count1)
min_count = count1;
/*
int next_p = tmp_place[x]+1;
int next_n = x+1;
if(next_p >= n)
next_p = 0;
if(next_n > n)
next_n = 1;
while(next_p != tmp_place[x])
{
if(tmp_place[next_n] != next_p)
{
int t = tmp_place[next_n];
tmp_place[next_n] = next_p;
tmp_place[tmp_array[next_p]] = t;
tmp_array[t] = tmp_array[next_p];
tmp_array[next_p] = next_n;
count1++;
}
next_p++;
next_n++;
if(next_p >= n)
next_p = 0;
if(next_n > n)
next_n = 1;
}
if(min_count > count1)
min_count = count1;
// 枚举降序的情况
// x,x-1,...,1,n,...,x+1
count1 = 0;
for(int i = 1; i <= n; i++)
tmp_place[i] = place[i];
for(int i = 0; i < n; i++)
tmp_array[i] = array[i];
next_p = tmp_place[x]+1;
next_n = x-1;
if(next_p >= n)
next_p = 0;
if(next_n < 1)
next_n = n;
while(next_p != tmp_place[x])
{
if(tmp_place[next_n] != next_p)
{
int t = tmp_place[next_n];
tmp_place[next_n] = next_p;
tmp_place[tmp_array[next_p]] = t;
tmp_array[t] = tmp_array[next_p];
tmp_array[next_p] = next_n;
count1++;
}
next_p++;
next_n--;
if(next_p >= n)
next_p = 0;
if(next_n < 1)
next_n = n;
}
if(min_count > count1)
min_count = count1;
*/
}
printf("%d\n", min_count);
}
return 0;
}
后来看了别人的思路,就是枚举所有可能出现的情况。然后计算交换次数。
交换次数的计算:将a1,a2,...,an变换至b1,b2,...,bn的方法是如果a1不为b1,将b1移到该位置,然后再看a2,依次类推。
交换次数为最少的原因是:如果ai不为bi, 那么至少需要一次交换将bi移到该位置。设bi原来所在的位置为bj. 如果ai为bj. 那么不需移动,如果ai不为bj, 那么需要移动bj一次。