Sort3 Problem Solving report--icedream61 Blog Park (reproduced please specify the source)
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Topic
Give you n, then give the number of n, and each number is one of them. Would you like to arrange this sequence in ascending order, at least a few 22 exchanges?
"Data Range"
1<=n<=1000
"Input Sample"
9
2
2
1
3
3
3
2
3
1
"Output Example"
4
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Analysis
Once the best strategy is simulated, the answer is as follows:
1, read n, and read n number D[1]~d[n], define the minimum number of exchanges required s=0
2, statistics under the number of different, get 1 of the number num[1] and 1, 2 of the total number num[2]. At this point, you can determine where the final positions are:
1:1~NUM[1]
2:NUM[1]+1~NUM[2]
3:num[2]+1~n
3, the first round of exchange, so that all 1 are in place:
I=1~NUM[1], looking for!=1 position, does not exist may wish to make i==num[1]+1
When the i==num[1]+1 shows that all 1 are in place, this round is over. Otherwise
J=NUM[1]+1~NUM[2], looking for ==1 position, does not exist may wish to make j==num[2]+1
K=num[2]+1~n, find ==1 position, not exist then may make j==n+1
Can come here to explain the need to exchange once, therefore ++s,
For the current I, first of all to seek a step in place (ie d[i]==2 and d[j], or d[i]==3 and d[k], if not possible to find a legal change can be, there is no legal change in the situation.
4, the second round of exchange, so that all 2 are in place (3 nature will be in place):
I=NUM[1]+1~NUM[2], looking for!=2 position, does not exist may wish to make i==num[2]+1
When the i==num[2]+1 shows that all 2 are in place, this round is over. Otherwise
J=num[2]+1~n, find ==2 position, not exist then may make j==n+1
Can come here to explain the need to exchange once, therefore ++s,
Exchange D[i] and D[j].
5. At this point, the two rounds of exchange completed, the resulting s is the minimum required number of exchanges.
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Summary
At first did not notice the casual exchange is not optimal, did not consider is the 3rd step in the analysis of the situation: in the first round should strive for one step, this can reduce the number of exchanges.
The topic also has a need to pay attention to the place (fast row, two-point method also often occurs in this problem), in order to let subscript do not go beyond the scope, at any time to determine whether cross-border.
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Code
1 /*2 id:icedrea13 Prob:sort34 lang:c++5 */6 7#include <iostream>8#include <fstream>9 using namespacestd;Ten One intn,d[1001]; A intnum[4]; - - intMain () the { -Ifstreaminch("sort3.in"); -Ofstream out("Sort3.out"); - + inch>>N; - for(intI=1; i<=n;++i) {inch>>d[i]; ++Num[d[i]];} +num[2]+=num[1]; A at //The situation of vector D: - //1:d[1]~d[num[1]] - //2:d[num[1]+1]~d[num[2]] - //3:d[num[2]+1]~d[n] - - ints=0; in for(intI=1, j=num[1]+1, k=num[2]+1;;) - { to while(i<=num[1] && d[i]==1) ++i; + if(i==num[1]+1) Break; - while(j<=num[2] && d[j]!=1) ++J; the while(K<=n && d[k]!=1) ++K; *++s; $ if(d[i]==2&& j<=num[2]) Swap (d[i],d[j]);Else if(d[i]==3&& k<=n) Swap (d[i],d[k]);//one step in place betterPanax Notoginseng Else if(j<=num[2]) Swap (d[i],d[j]);ElseSwap (d[i],d[k]);//If you can't one step, just change the legal one. - } the for(inti=num[1]+1, j=num[2]+1;;) + { A while(i<=num[2] && d[i]==2) ++i; the if(i==num[2]+1) Break; + while(J<=n && d[j]!=2) ++J; -Swap (d[i],d[j]); ++s; $ } $ - out<<s<<Endl; - the inch. Close (); - out. Close ();Wuyi return 0; the}
Usaco Section2.1 Sorting a three-valued Sequence problem solving report