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. Educational Codeforces Round 33

    # Who = Penalty * A B C D E F 479 arkethos 4 247   + 00:08 + 00:19 +1 00:59 +2 01:41     479  ne-leo ...

  2. hdu3416 Marriage Match IV 最短路+ 最大流

    此题的大意:给定一幅有向图,求起点到终点(都是固定的)的不同的最短路有多少条.不同的最短路是说不能有相同的边,顶点可以重复.并且图含有平行边. 看了题以后,就想到暴力,但是暴力往往是不可取的.(暴力的 ...

  3. MVC 入口

    1.在 Global.asax public class MvcApplication : System.Web.HttpApplication { protected void Applicatio ...

  4. mvc关于pots请求 哪个函数 出现bug研究

    这样能请求home下的updateA函数 这样能请求home下的update函数,不能请求updateA函数

  5. Java将数据以Excel文件形式导出后台代码实现

    下面代码实现所需jar包: tomcat-embed-core-8.5.11.jar: commons-lang3-3.0.1.jar: commons-io-2.5.jar: poi-3.9.jar ...

  6. SpringBoot事务注解详解

    @Transactional spring 事务注解 1.简单开启事务管理 @EnableTransactionManagement // 启注解事务管理,等同于xml配置方式的 <tx:ann ...

  7. Problem 13

    Problem 13 # Problem_13 """ Work out the first ten digits of the sum of the following ...

  8. 数据库优化一般思路(PLSQL、Navicat)

    SQL执行过程: 1.执行SQL时,sql解析引擎会被启动 2.数据类型和数据库表定义的数据类型不一致,数据库引擎会自动转化 3.数据库表定义了多个索引,sql引擎会帮你选择最优的一个 4.数据库引擎 ...

  9. ie6下position:fixed定位问题

    1. *html{ background-image:url(about:blank); background-attachment:fixed;}2.将需要用固定定位的元素中加上_position: ...

  10. Xshell 访问虚拟机中linux

    .关闭linux防火墙 service iptables stop chkconfig iptables off .启动ssh服务 service sshd start