The main topic: There are n aliens to the opening table meeting, the alien's number from 1 to N, requires the number of aliens adjacent to the location must sit in the number of i-1 and numbered i+1 aliens.
Now give the order of n aliens sitting on the round table, requiring you to go through a minimum of exchange (Exchange is the seat of two aliens exchange), so that all aliens sit in accordance with the rules of Appeal.
How to solve the problem: enumerate the positions of each alien sitting, assuming that the position must be a number 1 aliens, and then number from left to increment or decrement, and finally check the arrangement need to exchange several times aliens
If the alien sits in the wrong place, the aliens who are fit to sit in that seat are exchanged for the fewest number of exchanges.
#include <cstdio>#include <cstring>#define MAXN 510int POS[MAXN], START[MAXN], END[MAXN], N;intExchange () {intCNT =0; for(inti =1; I <= N; i++) {if(End[i]! = i) {POS[End[i]] =POS[i]; end[POS[i]] = end[i]; cnt++; } }returnCNT;} void Solve () {intMIN =0x3f3f3f3f; for(inti =1; I <= N; i++) { for(intj =1, k = i; J <= N; J + +, k++) {if(k > N) k =1; END[J] = start[k];POS[End[j]] = j; }intt = Exchange (); min = (T > MIN?) MIN:T); } for(inti =1; I <= N; i++) { for(intj =1, k = i; J <= N; J + +, k--) {if(k <=0) k = n; END[J] = start[k];POS[End[j]] = j; }intt = Exchange (); min = (T > MIN?) MIN:T); }printf("%d\ n", MIN);}intMain () { while(SCANF ("%d", &n) = =1&& N) { for(inti =1; I <= N; i++) scanf ("%d", &start[i]); Solve (); }return 0;}
UVA-10570 meeting with Aliens violence