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 题目描述 排序是一种很频繁的计算任务.现在考虑最多只有三值的排序问题.一个实际的例子是,当我们给某项竞赛的优胜者 ...
随机推荐
- Dapper中数据表的字段(列)与实体属性不一致时,如何手动配置它们之间的映射?
NET[C#]Dapper中数据表的字段(列)与实体属性不一致时,如何手动配置它们之间的映射? 问题描述 比如有如下的数据表结构:Person: person_id int first_name va ...
- CI中的url相关函数以及路由设置和伪静态技术
当使用CI框架进行开发时,我们的一些数据传递的URL不应该写死,可以使用如下方法:比如说我们需要表单提交一个数据: 1.在controller控制器中我们需要先创建一个加载helper和视图的方法: ...
- Visual Studio蛋疼问题解决(2)
Astyle配置 1.下载并安装Astyle(AstyleExtension.vsix),重新启动VS: 2.工具->选项,从左侧列表找到AStyleFormatter,在右边编辑参数,参考设置 ...
- 查询 MySQL 库/表相关信息
SHOW DATABASES //列出 MySQL Server 数据库. SHOW TABLES [FROM db_name] //列出数据库数据表. SHOW CREATE TABLES tbl_ ...
- (转)ORA-01502
问题:ora-01502 索引或这类索引的分区处于不可用状态 引发:移动数据表分区,导致索引失效 解决:重建失效索引 1. select index_name ,status from user_i ...
- DeepMind用ReinforcementLearning玩游戏
原文 : http://dataunion.org/?p=639 1.引言 说到机器学习最酷的分支,非Deep learning和Reinforcement learning莫属(以下分别简称DL和 ...
- 开启RxSwift之旅——开篇
开启RxSwift之旅——开篇 RxSwift 是 ReactiveX 在 Swift 下的实现.ReactiveX 是一个通过使用可观察序列来组合异步和基于事件的程序的库. 很多地方通常把 Reac ...
- RabbitMQ学习笔记(5)----RabbitMQ整合Spring
在Spring AMQP项目中Spring也提供了对RabbitMQ的支持,这里在之前学习SpringBoot的时候也整合过,但是今天这里使用的Spring的xml配置来整个rabbit. Sprin ...
- WEBGL学习【七】画布绘图
主要是对WEBGL的绘图部分进行了进一步加强的认识和理解 <!DOCTYPE HTML> <html lang="en"> <head> < ...
- [网络流24题] 最长k可重线段集问题 (费用流)
洛谷传送门 LOJ传送门 最长k可重区间集问题的加强版 大体思路都一样的,不再赘述,但有一些细节需要注意 首先,坐标有负数,而且需要开$longlong$算距离 但下面才是重点: 我们把问题放到了二维 ...