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. c基本语法和数据类型

    一.C语言结构 C 程序主要包括以下部分: 预处理器指令 函数 变量 语句 & 表达式 注释 二.基本语法 1.  分号是语句结束符. 2. 单行注释//.多行注释/* */. 3. 标识符是 ...

  2. css代码实现switch开关滑动

    效果预览: 代码如下: <style> #toggle-button{ display: none; } .button-label{ position: relative; displa ...

  3. c#在类里不能使用Response解决方法

    response对应的类是HttpResponse,  在System.Web 命名字间里,   如果你在类中要使用   Response 的话,   需要使用System.Web.HttpConte ...

  4. 《NVM-Express-1_4-2019.06.10-Ratified》学习笔记(6.5-加-8.2-加-8.3)End-to-end_Data_Protection

    6.5 End-to-end Protection Information 端到端保护信息 包含数据转存的命令可以使用端到端数据保护.在这些命令内部[位于Command Dword 12 的 [29: ...

  5. DataGridView只显示数据源中绑定的字段

    场景: 由于环境需要,在获取数据源的时候会获取多于DataGridView中绑定的字段,若不做任何处理,直接将数据源绑定到DataGridView上面,DataGridView就会将数据源中没有绑定的 ...

  6. 1级搭建类109-Oracle 12cR2 SI FS(Windows Server 2019)公开

    Oracle 12cR2 单实例文件系统在Windows Server 2019上的安装 在线查看

  7. Python学习笔记三:数据特征分析

    完成数据清理后,下面通过图表展开对数据的分析. 1.前期初判(分布分析): 1)判断分组区间: # a.散点图:plt.scatter(data[字段1],data['字段2'], s = data[ ...

  8. c# 删除功能

    html界面: js: controller: app:

  9. How to do high impact research + 实事求是

    1. develop a strong publications record early, so do what you can to make that happen. 2. 粗读:abstrac ...

  10. 二分-D - Can you solve this equation?

    D - Can you solve this equation? Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you ...