USACO 2.1 Sorting a Three-Valued Sequence
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的更多相关文章
- USACO Section2.1 Sorting a Three-Valued Sequence 解题报告
sort3解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...
- Test Precisely and Concretely
Test Precisely and Concretely Kevlin Henney IT IS IMPORTANT TO TEST for the desired, essential behav ...
- 【USACO 2.1】Sorting A Three-Valued Sequence
/* TASK: sort3 LANG: C++ URL: http://train.usaco.org/usacoprob2?a=RkPIMxsFWzm&S=sort3 SOLVE: n个数 ...
- USACO 2.1.3 Sorting a Three-Valued Sequence(sort3)
这道题就是给出由123三个值的一个数字序列,然后让你把这个序列升序排序,求最小的交换次数.注意这里可以不是相邻交换. 刚开始一看题的时候,还以为t=a a=b b=t那种水题呢,然后发现不是水题.. ...
- USACO Sorting a Three-Valued Sequence
题目描述 排序是一种很频繁的计算任务.现在考虑最多只有三值的排序问题.一个实际的例子是,当我们给某项竞赛的优胜者按金银铜牌排序的时候.在这个任务中可能的值只有三种1,2和3.我们用交换的方法把他排成升 ...
- USACO Section 2.1 Sorting a Three-Valued Sequence
/* ID: lucien23 PROG: sort3 LANG: C++ */ #include <iostream> #include <fstream> #include ...
- USACO Section 2.1 Sorting a Three-Valued Sequence 解题报告
题目 题目描述 给N个整数,每个整数只能是1,2,或3.现在需要对这个整数序列进行从小到大排序,问最少需要进行几次交换.N(1 <= N <= 1000) 样例输入 9 2 2 1 3 3 ...
- 洛谷P1459 三值的排序 Sorting a Three-Valued Sequence
P1459 三值的排序 Sorting a Three-Valued Sequence 166通过 369提交 题目提供者该用户不存在 标签USACO 难度普及- 提交 讨论 题解 最新讨论 那么 ...
- 洛谷 P1459 三值的排序 Sorting a Three-Valued Sequence
P1459 三值的排序 Sorting a Three-Valued Sequence 题目描述 排序是一种很频繁的计算任务.现在考虑最多只有三值的排序问题.一个实际的例子是,当我们给某项竞赛的优胜者 ...
随机推荐
- 查找DLL,并复制出来
Subst b: %windir%\assembly 执行完后,会发现硬盘分区多了个B盘,打开后看到了所有assembly下的DLL,于是在这里就搜到了Microsoft.ReportViewer.P ...
- function at line ### more than 60 upvalues
lua中函数的upvalues是有上限的,在luaconf.h中定义: /*@@ LUAI_MAXUPVALUES is the maximum number of upvalues per func ...
- JavaScript的面向对象
JavaScript的对象 对象是JavaScript的一种数据类型.对象可以看成是属性的无序集合,每个属性都是一个键值对,属性名是字符串,因此可以把对象看成是从字符串到值的映射.这种数据结构在其他语 ...
- mysql Seconds_Behind_Master
通过show slave status查看到的Seconds_Behind_Master,从字面上来看,他是slave落后master的秒数,一般情况下,也确实这样,通过Seconds_Behind_ ...
- 「Redis 笔记」常用命令
编号 命令 描述 1 DEL key 此命令删除一个指定键(如果存在). 2 DUMP key 此命令返回存储在指定键的值的序列化版本. 3 EXISTS key 此命令检查键是否存在. 4 EXPI ...
- iPhone Mach-O文件格式与代码签名
错误现象1) 直接运行/Applications/MobileFonex.app/MobileFonexKilled: 9 2)gdb调试Program received signal EXC_BAD ...
- JS 100以内的质数、只能被1和自己整除
for(var i = 2;i <= 100;i++){ var biao = 1; for(var j = 2;j < i;j++){ if(i%j == 0){ biao = 0; } ...
- Django:Admin,Cookie,Session
一. Admin的配置 1.Admin基础设置 admin是django强大功能之一,它能够从数据库中读取数据,呈现在页面中,进行管理.默认情况下,它的功能已经非常强大,如果你不需要复杂的功能,它已经 ...
- Centos文件查看命令字符
文件(夹)查看类命令 ls--显示指定目录下内容 说明:ls 显示结果以不同的颜色来区分文件类别.蓝色代表目录,灰色代表普通文件,绿色代表可执行文件,红色代表压缩文件,浅蓝色代表链接文件. -a--- ...
- hdu 1703
#include<stdio.h> #define N 11000 int a[N]; int main() { int i,n; a[1]=0;a[2]=5;a[3] ...