POJ 3270-cow Sorting (permutation group)

Source: Internet
Author: User

Title Address: POJ 3270

Test instructions: There are n cows, each cow has a unique "rage value", want to put their anger values from small to large (the time spent in exchanging any two cattle positions for their rage value and), the minimum Exchange time.

Ideas:

1. Find out the initial state and end state (initial state is given by the topic, end state is arranged from small to large)

2. Draw a permutation group and look for loops inside. For example

Initial state: 8 4 5 3 2 7

End Status: 2 3 4 5 7 8

Find 8->2->7->8 from the beginning, so a permutation group is (8,2,7), then remove the three points in both states, from the beginning to find 4->3->5->4, so another permutation group is (4,3,5).

3. Observe one of the loops, obviously, to minimize the exchange cost, you should use the smallest number 2 in the loop, go with the other two numbers, 7 and 8 to exchange. The cost of such an exchange is:
Sum-min + (len-1) * min
The sum of all the numbers in this loop, Len is the length, Min is the smallest number in the ring.

4. Considering the other situation, we can adjust a number from another loop to enter the loop, making the exchange less expensive. For example, the initial state:
1 8 9) 7 6
Can be decomposed into two loops:
(1) (8 6 9 7), obviously, the second loop is (8 6 9 7) and the smallest number is 6. We can deploy the smallest number 1 of the entire sequence into this loop. Change the second loop to: (8 1 9 7). Let this 1 complete the task, then swap with 6, and let 6 go back to the loop. The cost of doing this is obvious: Sum + min + (len + 1) * Smallest
The sum of all the numbers in this loop, Len is the length, Min is the smallest number in the ring, and smallest is the smallest number in the whole sequence.

5. Thus, the price of a loop is Sum-min + (len-1) * min and sum + min + (len + 1) * The small number in the smallest.

#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include < iostream> #include <sstream> #include <algorithm> #include <set> #include <queue> #include <stack> #include <map>//#pragma comment (linker, "/stack:102400000,102400000") using namespace Std;typedef Long long ll;const int inf=0x3f3f3f3f;const double pi= ACOs ( -1.0); const double ESP=1E-6;CONST int Maxn=10010;const int Max n1=100010; LL START[MAXN],END[MAXN];    LL has[maxn1];int VIS[MAXN]; int main () {int n,i;    LL res=0;    LL min1,min2;    LL sum,cnt;    int Begin;    memset (vis,0,sizeof (VIS));    while (~SCANF ("%d", &n)) {min1=inf;        for (i=1;i<=n;i++) {scanf ("%d", &start[i]);        End[i]=start[i];        Min1=min (Min1,start[i]);    Has[start[i]]=i;    } sort (end+1,end+n+1); for (i=1;i<=n;i++) {if (vis[i]| |        Start[i]==end[i]) continue;        Begin=i;        sum=cnt=0;        Min2=inf; while (!vis[has[end[Begin]])            {Begin=has[end[begin]];            Sum+=start[begin];            cnt++;            Vis[begin]=1;        Min2=min (Min2,start[begin]);    } res+=min (sum-min2+ (cnt-1) *min2,sum+min2+ (cnt+1) *min1);    } printf ("%lld\n", res); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 3270-cow Sorting (permutation group)

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.