Bzoj 1697: [usaco Feb] cow sorting ox sorting

Source: Internet
Author: User
Description

Farmer John is going to queue his n (1 <= n <= 10,000) cows in order to facilitate the operation. John wants to sort his temper by the size of his temper. The temper of each ox is an integer between 1 and 100,000, and the temper value of none of the two cows is the same. In the sorting process, John can exchange the positions of any two cows. John needs x + y seconds to exchange the temper values of X and Y. Please help John calculate the shortest time for sorting all steaks. Input

Row 1st: a number, N.

2nd ~ N + 1 rows: each row has one number, and row I + 1 is the temper value of the first ox. Output

Row 1st: a number, the shortest time for sorting all steaks.

Question:

Replacement group.

See hzwer

1. Find the initial and target statuses. Obviously, the target status is the sorted status.
2. Draw a replacement group and find a loop in it. For example, the number is 8 4 5 3 2 7
Obviously, the target status is 2 3 4 5 7 8, which can be written into two loops:
(8 2 7) (4 3 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
Simplified:
Sum + (LEN-2) * 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 cost of doing so is obvious:
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.

6. When calculating a loop, we do not need to record all the elements of the loop. We only need to record the minimum number and sum of the loop.

7. When storing data, we can use a hash structure to map the elements and their locations to know the elements and quickly reverse query the element location. In this way, you do not need to search one by one.

Code:

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>//by zrt//problem:using namespace std;typedef long long LL;const int inf(0x3f3f3f3f);const double eps(1e-9);int n;int to[100005];int a[10005];int b[10005];int mn;bool vis[10005];int solve(int x){    int sum=a[x];    vis[x]=1;    int k=x;    int minn=a[x];    int l=1;    for(k=to[b[k]];k!=x;k=to[b[k]]) minn=min(minn,a[k]),sum+=a[k],l++,vis[k]=1;    return min(sum+(l-2)*minn,sum+minn+(l+1)*mn);}int main(){    #ifdef LOCAL    freopen("in.txt","r",stdin);    freopen("out.txt","w",stdout);    #endif    scanf("%d",&n);    mn=inf;    for(int i=1;i<=n;i++){        scanf("%d",&a[i]);        to[a[i]]=i;        b[i]=a[i];        mn=min(a[i],mn);    }    sort(b+1,b+n+1);    int ans=0;    for(int i=1;i<=n;i++){        if(!vis[i]){            ans+=solve(i);        }    }    printf("%d\n",ans);    return 0;}

Bzoj 1697: [usaco Feb] cow sorting ox sorting

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.