Usaco Train 2.1.3 Sorting a three-valued Sequence (SORT3)

Source: Internet
Author: User

The problem is to give a sequence of numbers that are 1233 values, and then let you sort the sequence in ascending order to find the minimum number of exchanges. Note that this may not be an adjacent interchange.

At the beginning of a question, I thought t=a a=b b=t that water problem, and then found not water problem.

So I thought ... Since it is a sort question, first sort him out, then compare it.

For example, sample data on Usaco:

After sort before sorting

(1) 2 1
(2) 2 1
(3) 1 2
(4) 3 2
(5) 3 2
(6) 3 3
(7) 2 3
(8) 3 3
(9) 1 3

Since he is asking for a minimum number of times, we do not move data that is already in place, so we can delete data that is not moving in situ. The 6 and 8 here are data that is not moved. deleted, and then turned into:

(1) 2 1
(2) 2 1
(3) 1 2
(4) 3 2
(5) 3 2
(7) 2 3
(9) 1 3

This is where you can exchange 22, for example, position 1th and position 3rd can be exchanged 22. We define the positions that can be exchanged in 22 as cross-equal. The numbers 1th and 3rd are crossed equally, the 3rd and 7th are equally intersecting, so swapping them 22 will return to their original position. 22 swaps are exchanged only once, so in this step, the answers are exchanged +1 at a time until there is no more than 22 swap positions. And then it turns out:

(2) 2 1
(5) 3 2
(9) 1 3

We found that there are three groups left, in fact, each group of data filtering will become a multiple group of 3. Think about why. Because he will have 3 kinds of data (three-in-one), and there will be the first screening of the situation in situ, the second screening of the exchange of the situation, this time there is three data exchange situation. Why won't there be four data exchanges? Because he only has three data, where is your fourth data popping up ... Now that the rest is three data exchange, you don't have to look for it again, and you can calculate it directly with the total number of remaining data. Because each pair of three data exchange will change two times (you know the test), so this time to exchange the number of times (the remaining group number/3*2).

Since the idea is clear, on the code to put.

To explain, a is the input array, B is the sorted array, C is whether to be excluded, not ruled out is 0, is excluded is 1.

n is the total number of data entered, and M is the total number of remaining data (which will be continuously reduced). Ans is the answer, the total number of exchanges.

/*id:aaabbbr1lang:c++task:sort3*/#include <stdio.h> #include <string.h> #include <algorithm>using namespace Std; int A[1002];int B[1002];bool C[1002];int main () {freopen ("sort3.in", "R", stdin); Freopen ("Sort3.out", "w", stdout); int n , m;scanf ("%d", &n), m=n;//at the beginning, the number of remaining data equals the total number of data int ans=0;//answer if 0for (int i=1;i<=n;i++) {scanf ("%d", &a[i]); b[ The i]=a[i];//b array is used to sort the}sort (b+1,b+1+n);//Sort B array memset (C,0,sizeof (c));//c array is used to identify whether or not to be excluded for (int i=1;i<=n;i++)// Enumerate a change case {if (A[i]==b[i])//If data is in place {m--;//remaining data -1c[i]=1;//exclude data}}for (int i=1;i<=n;i++) for (int j=1;j<i;j++)// This can traverse any of the different combinations, the exhaustive method, the case of the poor lifting two
{if (a[i]==b[j]&&a[j]==b[i]&&c[i]!=1&&c[j]!=1)//means if the intersection equals and two are not excluded {m-=2;//data remainder minus 2ans +=1;//answer +1, need to swap swap (a[i],a[j]),//This sentence seems not necessary c[i]=1;//exclusion, or error c[j]=1;//ibid}}ans+=m/3*2;//This is 3 of the situation printf ("%d\n", ans) ; fclose (stdin); fclose (stdout); return 0;}

Usaco Train 2.1.3 Sorting a three-valued Sequence (SORT3)

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.