UVA-10570 meeting with Aliens violence

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.