Rokua P1459 of three values sorting a three-valued Sequence

Source: Internet
Author: User

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

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.