UVA 11925 generating permutations generation arrangement

Source: Internet
Author: User

Test instructions: To generate a given sequence with an ordered sequence, there are two operations, one for the first two elements, and the other to move the first element to the last.

There are two ways of thinking:

1. Map, map the given sequence to an ordered sequence, then map the ordered sequence to the same substitution rules, and then you can sort it out.

Specific explanations can be seen in the C Problem of SRM 664

2. Reverse thinking, the given sequence into order, the operation of the corresponding change, the final reverse output operation.

As for the sorting problem, consider the sequence as a ring, the second operation quite changes the position of the exchangeable element, then it can be equivalent to bubble sort ...

The second way to note is that because it is annular, and bubble sort is different, the largest element in the head can not be exchanged, or into the dead loop, the largest element is located in the same position as the chain of the last order area, it is not necessary to compare.

Judgment termination can first find the total number of reverse order, exchange an inverse number minus one

#include <bits/stdc++.h>using namespacestd;inta[ the],t[ the],INV;voidMerge_sort (intLintR) {    if(L = = r)return; intMid = (l+r) >>1;    Merge_sort (L,mid); Merge_sort (Mid+1, R); intp = L, q = r, k =BL;  while(P <= mid && Q <=R) {        if(a[p]>A[q]) {INV+ = Mid-p +1; T[k+ +] = a[q++]; } Else{t[k+ +] = a[p++]; }    }    if(P>mid) for(inti = q; I <= R; i++) t[k++] =A[i]; Else  for(inti = q; I <= mid; i++) t[k++] =A[i];  for(k = l; k <= R; k++) A[k] =t[k];}intMain () {//freopen ("In.txt", "R", stdin);    intN, b[ the];  while(SCANF ("%d",&N), N) { for(inti =0; I < n; i++) {            intT scanf"%d",&t); B[t-1] =i; } memcpy (A, B,sizeof(int)*N); INV=0; Merge_sort (0, N-1);  for(inti = n1; I >=1&& INV; i--){             for(intj =0; J < I; J + +){                if(B[j] > b[j+1]) {Swap (B[j],b[j+1]); Putchar ('1'); INV--; } Putchar ('2'); }            //continue move I-1             for(intj = i; J < N; j + +) {Putchar ('2'); }} putchar ('\ n'); }    return 0;}

UVA 11925 generating permutations generation arrangement

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.