Number of "sort" cows

Source: Internet
Author: User

Title Description

There are N (1≤n≤1000) cows, which are labeled with a priority number: 3. Used to indicate their priority when drinking water, the highest priority numbered L, number 2 followed by the last number 3. Every day the cows start out in a row, but it's always messy, and you need to rearrange them into 1 cows at the top, numbered 2 second, and the cows numbered 3 at the end. Can you figure out the minimum number of switching orders to complete this rearrangement?

Input

Line 1th: an integer n;
2nd to N+i: line I+l has an integer indicating the number of the first cow in the start queue.

Output

1 lines, only one integer, indicates the minimum number of interchanges required.

Sample input
9221333231

Sample output
4

Tips

Sample Description: There is an Exchange method.
2 2 2< 1 1
2< 1 1 1 1
1< 2 2 2 2
3 3< 2 2 2
3 3 3 3< 2
3 3 3) 3 3
2 2< 3 3 3
3 3 3) 3 3
1 1 1< 2< 3

We can define a cnt[] to record the number of the two-and-one, and then block to discuss. First in the area of 1 (ie, 1~cnt[1]) traversal, if a[i]==2, then go to 2 of the area to find 1 and replace, if 2 of the area is not 1, go to 3 of the area, if a[i]==3, then go to 3 of the area to find 1 and replace, if 3 of the area is not 1, go to 2 of the area. Then in the area of 2 (i.e. cnt[1]+1~cnt[1]+cnt[2]), as we have ensured that 1 of the area is only 1, then 2 can only appear in the area of 2 and 3, so we just need to see how many 2 in this area of 3.

1#include <bits/stdc++.h>2 using namespacestd;3typedefLong Longll;4 intn,a[1005],cnt[5];5 intMain ()6 {7Cin>>N;8      for(intI=1; i<=n;i++)9     {TenCin>>A[i]; Onecnt[a[i]]++; A     } -     intans=0; -      for(intI=1; i<=cnt[1];i++) the     { -         if(a[i]==2) -         { -              for(intj=cnt[1]+1; j<=cnt[2]+cnt[1];j++) +             { -                 if(a[j]==1) +                 { Aans++; at swap (a[i],a[j]); -                      Break; -                 } -             } -             if(a[i]==2) -             { in                  for(intj=cnt[1]+cnt[2]+1; j<=n;j++) -                 { to                     if(a[j]==1) +                     { -ans++; the swap (a[i],a[j]); *                          Break; $                     }Panax Notoginseng                 } -             } the         } +         Else if(a[i]==3) A         { the              for(intj=cnt[1]+cnt[2]+1; j<=n;j++) +             { -                 if(a[j]==1) $                 { $ans++; - swap (a[i],a[j]); -                      Break; the                 } -             }Wuyi             if(a[i]==3) the             { -                  for(intj=cnt[1]+1; j<=cnt[2]+cnt[1];j++) Wu                 { -                     if(a[j]==1) About                     { $ans++; - swap (a[i],a[j]); -                          Break; -                     } A                 } +             } the         } -     } $      for(inti=cnt[1]+1; i<=cnt[1]+cnt[2];i++) the     { the         if(a[i]!=2) theans++; the     } -cout<<ans<<Endl; in}
View Code

Number of "sort" cows

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.