POJ 3270-Cow Sorting (replacement group), poj3270-cow

Source: Internet
Author: User

POJ 3270-Cow Sorting (replacement group), poj3270-cow

Address: POJ 3270

Question: There are nheaded cows, each of which has a unique "anger value", to sort their anger values from small to large (the time spent exchanging any two cows for their anger values and), find the minimum exchange time.

Ideas:

1. Locate the initial and end states (the initial states are given by the question, and the end States are arranged from small to large)

2. Draw a replacement group and find a loop in it. For example

Initial status: 8 4 5 3 2 7

End status: 2 3 4 5 7 8

Start from scratch to find 8-> 2-> 7-> 8, so a replacement group is (8, 2, 7), and then remove these three points in two states, find 4-> 3-> 5-> 4 from the beginning, so the other replacement group is (, 5 ).

3. observe one of the loops. Obviously, to minimize the exchange cost, use the smallest number 2 in the loop to exchange with the other two numbers 7 and 8. The price of this exchange is:
Sum-min + (len-1) * min
Sum is the sum of all numbers in the loop, len is the length, and min is the smallest number in the ring.
 
4. Considering another situation, we can call a number from another loop to enter this loop, reducing the exchange cost. For example, initial status:
1 8 9 7 6
It can be divided into two cycles:
(1) (8 6 9 7), obviously, the second cycle is (8 6 9 7), and the minimum number is 6. We can draw the smallest number 1 in the entire series to enter this loop. Change the second cycle to (8 1 9 7 ). Let this 1 complete the task, and then exchange with 6, let 6 return to the loop again. The obvious cost is: sum + min + (len + 1) * smallest
Sum is the sum of all the numbers in the cycle, len is the length, min is the smallest number in the ring, and smallest is the smallest number in the entire series.
 
5. therefore, the cost of sorting a loop is sum-min + (len-1) * min and sum + min + (len + 1) * small number of 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 Maxn1=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 Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.