P1459 of three values sorting a three-valued SequenceTitle Description
Sorting is a very frequent computational task. Now consider sorting problems with up to three values. A practical example is when we give a winner of a competition a sort of gold and silver medal. The possible values in this task are only three and 3. We put him in ascending order by means of an exchange.
Write a program that calculates the minimum number of exchanges required for a given sequence of one-and-a-line numbers, in ascending order
input/output formatInput Format:
First line:
Number of medals N (1 <= n <= 1000)
Line 2nd to section n+1:
Each line is a number that represents the medal. Total n rows. (1.. 3)
output Format:
A total of one row, a number. Represents the minimum number of exchanges required to rank in ascending order.
input/Output sampleInput Sample # #: Copy
9221333231
Output Example # #: Replication
4
Description
Usaco 2.1
Translation from Nocow
Idea: Greed.
#include <map>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;intN,ans;inta[10000];intnum[10000],pos[10000];intMain () {scanf ("%d",&N); for(intI=1; i<=n;i++) scanf ("%d", &num[i]), pos[num[i]]++; for(intI=1; i<=pos[1]+pos[2];i++){ if(num[i]==3) a[3]++; Else if(num[i]==2&&i<=pos[1]) a[1]++; Else if(num[i]==1&&i>pos[1]) a[2]++; } cout<<a[3]+ (a[1]>a[2]?a[1]:a[2]) <<Endl;}
Rokua P1459 of three values sorting a three-valued Sequence