bzoj1697:[usaco2007 feb]cow sorting cattle Sort & Bzoj1119:[poi2009]slo

Source: Internet
Author: User
Tags cmath

Ideas: Take bzoj1119 as an example, the topic has been given a replacement, and each exchange of the cost is to exchange the sum of the weights of the two, and the replacement must be produced some ring, so that you can only use one element in the ring to displace and so that all the remaining elements are in the correct position, obviously to choose the smallest number in the ring, But there may also be a number that makes it not within the currently processed ring and it is swapped with a number in the current ring, then swapped within the loop, and then swapped back the previous number, so it is also obvious to choose the smallest of all elements and the smallest in the current loop to exchange, and then take a min. It is also important to note that it is possible that the smallest of the current rings is the smallest of all elements, and a special sentence can be given.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cmath>6 using namespacestd;7 #defineMAXN 10001008 #defineINF 1e99  Ten intN,minval=Inf,tot; One Long Longans; A intA[MAXN],POS[MAXN],B[MAXN],VAL[MAXN],NEXT[MAXN]; - BOOLVIS[MAXN]; -   the intRead () { -     intx=0, f=1;CharCh=GetChar (); -      for(;ch<'0'|| Ch>'9'; Ch=getchar ())if(ch=='-') f=-1; -      for(; ch>='0'&& ch<='9'; Ch=getchar ()) x=x*Ten+ch-'0'; +     returnx*F; - } +   A intMain () { atn=read (); -      for(intI=1; i<=n;i++) Val[i]=read (), minval=min (minval,val[i]); -      for(intI=1; i<=n;i++) A[i]=read (), pos[a[i]]=i; -      for(intI=1; i<=n;i++) B[i]=read (), next[pos[a[i]]]=Pos[b[i]]; -      for(intI=1; i<=n;i++) -         if(!Vis[i]) { in             Long Longsum=0;intSize=0, v=inf; -              while(!vis[i]) vis[i]=1, Size++,sum+=val[a[i]],v=min (V,val[a[i]), i=Next[i]; to             if(size<=1)Continue; +             Long Longans1=sum-v+1ll*v* (size-1), ans2=sum+v+1ll*minval* (size+1); -             if(v!=minval) ans+=min (ANS1,ANS2);Elseans+=ans1; the         } *printf"%lld\n", ans); $}
bzoj1119
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cmath>6 using namespacestd;7 #defineMAXN 1001008 #defineINF 1e99  Ten intN,minval=Inf,ans; One intTMP[MAXN],VAL[MAXN],POS[MAXN],NEXT[MAXN]; A BOOLVIS[MAXN]; -   - intRead () { the     intx=0, f=1;CharCh=GetChar (); -      for(;ch<'0'|| Ch>'9'; Ch=getchar ())if(ch=='-') f=-1; -      for(; ch>='0'&& ch<='9'; Ch=getchar ()) x=x*Ten+ch-'0'; -     returnx*F; + } -   + intMain () { An=read (); at      for(intI=1; i<=n;i++) Tmp[i]=val[i]=read (), pos[val[i]]=i,minval=min (minval,val[i]); -Sort (tmp+1, tmp+n+1); -      for(intI=1; i<=n;i++) next[pos[tmp[i]]]=Pos[val[i]]; -      for(intI=1; i<=n;i++){ -         intsum=0, size=0, v=inf; -          while(!vis[i]) vis[i]=1, Size++,sum+=val[i],v=min (V,val[i]), i=Next[i]; in         if(size<=1)Continue; -         intans1=sum+v* (size-2), ans2=sum+minval* (size+1)+v; to         if(minval!=v) ans+=min (ANS1,ANS2);Elseans+=ans1; +     } -printf"%d\n", ans); the     return 0; *}
bzoj1697

bzoj1697:[usaco2007 feb]cow sorting cattle Sort & Bzoj1119:[poi2009]slo

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.