Basic Application of replacement (with the poj 3270 cow sorting solution Report)

Source: Internet
Author: User


For n given numbers, we can consider any arrangement as a replacement. So for the question poj 3270, let's look at this: first, we will give you an order of n different numbers, and then we will sort these numbers from small to small. The question requires an ascending order. Then we regard the input arrangement as a replacement.

Therefore, any replacement can be expressed by multiplying the replacement ring. Therefore, in order for the number in the replacement ring to return to its original position, each number must be exchanged at least once. When we use any number in the replacement ring for multiple exchanges, we can certainly restore all the numbers to a proper position, however, in order to minimize the sum of the two numbers during the exchange, we chose to replace the smallest element in the ring for multiple exchanges, this method is the best solution in a ring. The result can be expressed as sum + (len-2) * minnum, where sum represents the sum of all elements in the replacement ring, Len represents the number of elements in the replacement ring, and minnum represents the smallest element in the replacement ring.

Of course, in addition to using the elements in the replacement ring for multiple exchanges so that the elements return to their original positions, we can also select any element from the outside to replace the smallest element in the ring for multiple exchanges, but at this time, we need to introduce the minimum element in the ring and the extra two exchanges and sums of the element. So what should we choose from the element outside? Obviously, in order to optimize the solution, we must replace the smallest element in the entire sequence with the smallest element in the ring for exchange. The cost of the exchange at this time is: sum-minnum + small + (len-2) * Small + minnum * 2 + small * 2 that is: Sum + minnum + (LEN + 1) * Small, small is the smallest element in the entire series.

So after analyzing so much, the optimal solution for each replacement ring is the smaller one of the above two solutions ...... The following code is attached:

#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <algorithm>#include <vector>using namespace std;const int Max=100010;vector<int> adj[Max];int vis[Max];void init(){    for(int i=1;i<=Max;i++)        adj[i].clear();    memset(vis,0,sizeof(vis));}int a[10010],b[10010];int bfs(int s,int small){    int ss=s;    int minNum=s;    int sum=s;    int d=1;  //  printf("begin %d ",ss);    while(adj[s][0]!=ss)    {    //    printf("%d ",adj[s][0]);        d++;        minNum=min(minNum,adj[s][0]);        s=adj[s][0];        vis[s]=1;        sum+=s;    }    int tsum=sum+minNum+(d+1)*small;    sum+=(d-2)*minNum;    return min(sum,tsum);}int main(){    int n,i;    int ans;    int small;    while(scanf("%d",&n)!=EOF)    {        init();        small=0xFFFFFFF;        for(i=0;i<n;i++)        {            scanf("%d",&a[i]);            b[i]=a[i];            small=min(small,a[i]);        }        sort(a,a+n);        ans=0;        for(i=0;i<n;i++)        {            adj[a[i]].push_back(b[i]);        }        for(i=0;i<n;i++)        {            if(vis[a[i]]==0)            {                vis[a[i]]=1;                ans+=bfs(a[i],small);            }        }        printf("%d\n",ans);    }    return 0;}

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.