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.

本来打了贪心得进行模拟,后来改成了如下玄学贪心做法,十分简洁。欢迎Hack。

统计1,2,3的个数,可知放置1,2,3正确的位置,统计在各位置的数字1,2和3的个数,具体详见下。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int MAXN=1000+5;
int ct[4],c[4][4];
int a[MAXN]; int main()
{
freopen("sort3.in","r",stdin);
freopen("sort3.out","w",stdout);
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i)
scanf("%d",&a[i]),++ct[a[i]];
for(int i=1;i<=ct[1];++i)
++c[1][a[i]];
for(int i=ct[1]+1;i<=ct[1]+ct[2];++i)
++c[2][a[i]];
//统计个数
int ans=0;
ans+=c[1][2]+c[1][3];//把1换到正确位置的最少步数
c[2][3]+=max(0,c[2][1]-c[1][2]);//有多少3被交换到了二位置
ans+=c[2][3];//将2,3交换到正确位置
printf("%d\n",ans);
return 0;
}

三值的排序 Sorting a Three-Valued Sequence(洛谷 P1459USACO2.1,IOI96Day2)的更多相关文章

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

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

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

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

  3. P1459 三值的排序 Sorting a Three-Valued

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

  4. Sorting a Three-Valued Sequence(三值的排序)

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

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

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

  6. 【USACO 2.1.3】三值的排序

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

  7. 【算法•日更•第三十九期】迭代加深搜索:洛谷SP7579 YOKOF - Power Calculus 题解

    废话不多说,直接上题: SP7579 YOKOF - Power Calculus 题意翻译 (略过没有营养的题干) 题目大意: 给出正整数n,若只能使用乘法或除法,输出使x经过运算(自己乘或除自己, ...

  8. Sorting a Three-Valued Sequence(三值排序)

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

  9. USACO2.1.3 三值排序

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

随机推荐

  1. Linux的常用命令---这是对Linux最基本的尊重

    Linux: 诞生日期:1991年 开发者:林纳斯·托瓦茨 特点:免费,开源 发行版本:centos|red Hat|Ubuntu|红旗等 思想:一切都是文件 重要文件目录 bin:二进制文件(命令) ...

  2. springboot整合PageHelper

    1.在pom文件中引入Pagehelper分页插件 <!-- 分页插件 --> <dependency> <groupId>com.github.pagehelpe ...

  3. 搭建 Review Board - SVN 审核工具

    一.安装环境 CentOS-6.7,ReviewBoard-2.5.1.1 二.安装环境的配置 1.确认当前系统中有如下包,若没有,使用yum安装 httpd-2.2.15:httpd 指的是apac ...

  4. JFinal Enjoy指令扩展管理常用文本模板

    个人博客 地址:http://www.wenhaofan.com/article/20190304102258 平时在项目中使用短信模板 邮件模板以及 站内消息通知等文本模板一般都是通过手动的字符串拼 ...

  5. 码云项目克隆至github

    个人博客 地址:http://www.wenhaofan.com/article/20181104211917 因为我的博客项目一开始是存放在码云上面的,但是我又想在GitHub上有该项目的提交记录, ...

  6. 使用饿了么el-date-picker里及如何将后台给的时间戳js转化为时间格式

    首先代码是这个样子的,使用v-model <el-date-picker v-model="formData.createTime" :disabled="true ...

  7. 条件锁Condition

    """设计场景:timo先说一句,亚索再说一句timo: timo队长正在待命yasuo: 面对疾风吧timo: timo整装待发yasuo: 哈杀gay "& ...

  8. 四、CentOS 7安装Oracle JDK

    CentOS 7安装Oracle JDK,查看Linux是否自带的JDK,如有openJDK,则卸载  CentOS7.1 JDK安装 1.卸载自带OPENJDK    用 java -version ...

  9. HDU 1542 Atlantis(扫描线算法)

    题意:给出n个矩形的左下角左边和右上角坐标,求这n个矩形的面积并 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 典型的扫描线算法的题目 什么是 ...

  10. 题解【洛谷P1967】[NOIP2013]货车运输

    题面 题解 注意到有一些限重很低的边不会被走到. 于是考虑建一棵最大生成树,在生成树上寻找答案. 设\(f[i][j]\)表示\(i\)的\(2^j\)级祖先,\(w[i][j]\)表示\(i\)到\ ...