Sorting a Three-Valued Sequence 
IOI'96 - Day 2

Sorting is one of the most frequently performed computational tasks. Consider the special sorting problem in which the records to be sorted have at most three different key values. This happens for instance when we sort medalists of a competition according to medal value, that is, gold medalists come first, followed by silver, and bronze medalists come last.

In this task the possible key values are the integers 1, 2 and 3. The required sorting order is non-decreasing. However, sorting has to be accomplished by a sequence of exchange operations. An exchange operation, defined by two position numbers p and q, exchanges the elements in positions p and q.

You are given a sequence of key values. Write a program that computes the minimal number of exchange operations that are necessary to make the sequence sorted.

PROGRAM NAME: sort3

INPUT FORMAT

Line 1: N (1 <= N <= 1000), the number of records to be sorted
Lines 2-N+1: A single integer from the set {1, 2, 3}

SAMPLE INPUT (file sort3.in)

9
2
2
1
3
3
3
2
3
1

OUTPUT FORMAT

A single line containing the number of exchanges required

SAMPLE OUTPUT (file sort3.out)

4

题目大意:三值排序,就是说一个有N个数字的序列(数字的范围是1到3),现在想排成升序,问最少几次交换。
思路:大概的想法,最优的可以交换的数字有两种,第一种是交换后两个都到了自己该在的位置比如(2,1,3),第二种是需要交换“一轮”,比如(3,2,1),第一种的代价是一,第二种的代价是2,总的思路就是先确定三个值排序结束后各个数字该在的范围,然后找出各个位置不对的数字的个数(比如a2代表在1排完序该在的位置上有a2个2,a3代表有几个3,b代表2排完序的位置或者说长度),先把可以两两交换的交换完,然后剩下的都是循环交换的,加起来除以三乘2就好,代码如下,略丑
 /*
ID:fffgrdc1
PROB:sort3
LANG:C++
*/
#include<cstdio>
#include<iostream>
using namespace std;
int a[];
int main()
{
freopen("sort3.in","r",stdin);
freopen("sort3.out","w",stdout);
int n,ans=;
scanf("%d",&n);
int a2=,a3=,b1=,b3=,c1=,c2=,al=,bl=,cl=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]==)al++;
else if(a[i]==)bl++;
else cl++;
}
for(int i=;i<=al;i++)
{
if(a[i]==)
{
a2++;
}
else if(a[i]==)a3++;
}
for(int i=al+;i<=al+bl;i++)
{
if(a[i]==)b1++;
else if(a[i]==)b3++;
}
for(int i=al+bl+;i<=al+bl+cl;i++)
{
if(a[i]==)c1++;
else if(a[i]==)c2++;
}
//printf("%d %d %d %d %d %d %d %d %d\n",al,a2,a3,bl,b1,b3,cl,c1,c2);
int temp=min(c2,b3);
ans+=temp;c2-=temp;b3-=temp;
temp=min(c1,a3);
ans+=temp;c1-=temp;a3-=temp;
temp=min(a2,b1);
ans+=temp;a2-=temp;b1-=temp;
temp=a2+a3+b1+b3+c1+c2;
temp/=;
temp*=;
ans+=temp;
//printf("%d %d %d %d %d %d %d %d %d\n",al,a2,a3,bl,b1,b3,cl,c1,c2);
printf("%d\n",ans);
return ; }

看了其他人的题解,和我的思路大同小异。这题没什么意思。。。

USACO 2.1 Sorting a Three-Valued Sequence的更多相关文章

  1. USACO Section2.1 Sorting a Three-Valued Sequence 解题报告

    sort3解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...

  2. Test Precisely and Concretely

    Test Precisely and Concretely Kevlin Henney IT IS IMPORTANT TO TEST for the desired, essential behav ...

  3. 【USACO 2.1】Sorting A Three-Valued Sequence

    /* TASK: sort3 LANG: C++ URL: http://train.usaco.org/usacoprob2?a=RkPIMxsFWzm&S=sort3 SOLVE: n个数 ...

  4. USACO 2.1.3 Sorting a Three-Valued Sequence(sort3)

    这道题就是给出由123三个值的一个数字序列,然后让你把这个序列升序排序,求最小的交换次数.注意这里可以不是相邻交换. 刚开始一看题的时候,还以为t=a a=b b=t那种水题呢,然后发现不是水题.. ...

  5. USACO Sorting a Three-Valued Sequence

    题目描述 排序是一种很频繁的计算任务.现在考虑最多只有三值的排序问题.一个实际的例子是,当我们给某项竞赛的优胜者按金银铜牌排序的时候.在这个任务中可能的值只有三种1,2和3.我们用交换的方法把他排成升 ...

  6. USACO Section 2.1 Sorting a Three-Valued Sequence

    /* ID: lucien23 PROG: sort3 LANG: C++ */ #include <iostream> #include <fstream> #include ...

  7. USACO Section 2.1 Sorting a Three-Valued Sequence 解题报告

    题目 题目描述 给N个整数,每个整数只能是1,2,或3.现在需要对这个整数序列进行从小到大排序,问最少需要进行几次交换.N(1 <= N <= 1000) 样例输入 9 2 2 1 3 3 ...

  8. 洛谷P1459 三值的排序 Sorting a Three-Valued Sequence

    P1459 三值的排序 Sorting a Three-Valued Sequence 166通过 369提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 那么 ...

  9. 洛谷 P1459 三值的排序 Sorting a Three-Valued Sequence

    P1459 三值的排序 Sorting a Three-Valued Sequence 题目描述 排序是一种很频繁的计算任务.现在考虑最多只有三值的排序问题.一个实际的例子是,当我们给某项竞赛的优胜者 ...

随机推荐

  1. Surround the Trees[HDU1392]

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. Java集合(一)HashMap

    HashMap 特点: HashMap的key和value都允许为空,无序的,且非线程安全的 数据结构: HashMap底层是一个数组,数组的每一项又都是链表,即数据和链表的结合体.当新建一个Hash ...

  3. 如何修改 WordPress 数据库前缀

    我们知道 WordPress 的数据库表,可以设置前缀,默认是 wp_,很多同学也就默认用了 wp_,如果某种原因(比如提高安全性)要修改的 WordPress 数据的前缀,我们应该怎么做? 开始之前 ...

  4. JQuery学习笔记系列(一)----选择器详解

    笔者好长时间没有更新过博客园的笔记了,一部分原因是去年刚刚开始工作一段时间忙碌的加班,体会了一种每天加班到凌晨的充实感,之后闲暇时间了也因为自己懒惰没有坚持记笔记的习惯,现在重新拾起来. 借用古人的一 ...

  5. SqlServer数据库表导入SqlLite数据库表保持日期时间类型字段的格式

    在写查询功能的过程中遇到一个这样的问题:按日期范围查询,sql语句是:where dt>=用户选择起始日期&&dt<=用户选择结束日期.数据库中的数据如图1,我选择的测试数 ...

  6. Verilog之$sreadmemb

    1 Memories  Memories file format is shown below, the address is specified as @ <address> in he ...

  7. 图像的全局特征--LBP特征

    原文链接:http://blog.csdn.net/zouxy09/article/details/7929531#comments 这个特征或许对三维图像特征提取有很大作用.文章有修改,如有疑问,请 ...

  8. Kafka学习笔记(6)----Kafka使用Producer发送消息

    1. Kafka的Producer 不论将kafka作为什么样的用途,都少不了的向Broker发送数据或接受数据,Producer就是用于向Kafka发送数据.如下: 2. 添加依赖 pom.xml文 ...

  9. H3C三层交换机S5500初始配置+网络访问策略

    DHCP中继配置命令 dhcp relay address-check enable 命令用来使能DHCP 中继的地址匹配检查功能. undo dhcp relay address-check ena ...

  10. vue 动态添加路由 require.context()

    之前的写法 'use strict' import Vue from 'vue' import MessageBroadcast from 'page/MessageBroadcast' import ...