Bzoj1697: [usaco Feb] cow sorting ox sorting

Source: Internet
Author: User
1697: [usaco Feb] cow sorting ox sorting time limit: 5 sec memory limit: 64 MB
Submit: 387 solved: 215
[Submit] [Status] 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.

Sample input3
2
3
1

Input explanation:

There are three cows in the queue and their temper is 2, 3, and 1 respectively.
Sample output7

Output explanation:
2 3 1: Initial Sequence
2 1 3: Exchange temper for 3 and 1 cows (time = 1 + 3 = 4 ).
1 2 3: Exchange temper for 1 and 2 cows (time = 2 + 1 = 3 ).
Hint Source

Gold

Question: The original question of the black book, The Cost of sorting. Code:
 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set>10 #include<queue>11 #include<string>12 #define inf 100000000013 #define maxn 1500014 #define maxm 500+10015 #define eps 1e-1016 #define ll long long17 #define pa pair<int,int>18 #define for0(i,n) for(int i=0;i<=(n);i++)19 #define for1(i,n) for(int i=1;i<=(n);i++)20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)21 using namespace std;22 inline int read()23 {24     int x=0,f=1;char ch=getchar();25     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}26     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}27     return x*f;28 }29 ll n,mi,cs=0,a[maxn],b[maxn],c[maxn];30 bool check[maxn];31 inline bool cmp(int x,int y)32 {33     return a[x]<a[y];34 }35 int main()36 {37     freopen("input.txt","r",stdin);38     freopen("output.txt","w",stdout);39     n=read();40     for1(i,n)a[i]=read(),b[i]=i;41     sort(b+1,b+n+1,cmp);42     for1(i,n)c[b[i]]=i;43     ll ans=0;44     mi=a[b[1]];45     for1(i,n)46      if(!check[i])47       {48           ll j=i,len=0,tmp=inf,sum=0;49           while(!check[j])50           {51            check[j]=1;52            tmp=min(tmp,a[j]);53            sum+=a[j];54            len++;55            j=c[j];56           } 57         ans+=sum+min((len-2)*tmp,mi*(len+1)+tmp);  58       }59     cout<<ans<<endl;60     return 0;61 }
View code

 

Bzoj1697: [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.