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. webkitdirectory 实现文件夹上传

    文件夹上传这个功能在web端可能有需求,这里就简单介绍下用法. 目前只有谷歌浏览器还有Microsoft Edge支持按照文件夹进行上传,具体可以看下百度云盘的网页版的上传按钮,在火狐下就支持按照文件 ...

  2. HBase编程 API入门系列之工具Bytes类(7)

    这是从程度开发层面来说,为了方便和提高开发人员. 这个工具Bytes类,有很多很多方法,帮助我们HBase编程开发人员,提高开发. 这里,我只赘述,很常用的! package zhouls.bigda ...

  3. 二.Windows I/O模型之异步选择(WSAAsyncSelect)模型

    1.基于windows消息为基础的网络事件io模型.因此我们必须要在窗口程序中使用该模型.该模型中的核心是调用WSAAsyncSelect函数实现异步I/O. 2.WSAAsyncSelect函数:注 ...

  4. Java 方法的应用

    Java方法可以理解为C#中的函数,都是把复杂的问题简单化,按模块,按功能区分,分别完成各个部分在调用这些方法完成整个功能. 方法的综合练习,猜数字的实现: 代码要求: 生成不重复的4位数字(只有1- ...

  5. css3动画,点击圆形背景扩展整个页面效果

    上次做项目的时候,要求点击链接,这个链接的圆形背景扩散充满整个页面,今天把这个效果整理一下,是简单的css3的动画特效,粘贴下面的代码看效果 <!DOCTYPE html> <htm ...

  6. Dynamics CRM 使用 Profiler 来做debug

    首先,我们需要install Profiler 我们选中一个plugin, 并且选择start Profilling 然后我们选择Persist to Entity 然后我们执行trigger这个pl ...

  7. 基于Nginx的SSL虚拟主机

    通过私钥,证书对站点www.test.com的所有数据加密,实现通过https访问www.test.com 环境说明: 源码安装Nginx时必须使用--with-http_ssl_module参数,启 ...

  8. .apply .call方法的区别及使用 .apply第二个参数为数组,.call第二个参数为参数列表, 相同点:第一个参数都为Function函数内部的this对象.

    Function.apply(obj,args)方法能接收两个参数 obj:这个对象将代替Function类里this对象 args:这个是数组,它将作为参数传给Function(args--> ...

  9. Arguments Optional FreeCodeCamp

    function add() { if(typeof arguments[0] !== "number" || (arguments.length > 1 && ...

  10. bzoj 1293: [SCOI2009]生日礼物 问题转化 + 性质分析 + 滚动数组优化

    Description 小西有一条很长的彩带,彩带上挂着各式各样的彩珠.已知彩珠有N个,分为K种.简单的说,可以将彩带考虑为x轴,每一个彩珠有一个对应的坐标(即位置).某些坐标上可以没有彩珠,但多个彩 ...