usaco2.12Sorting a Three-Valued Sequence
来源:互联网 发布:c语言单向链表的建立 编辑:程序博客网 时间:2024/06/02 13:37
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 sortedLines 2-N+1:A single integer from the set {1, 2, 3}SAMPLE INPUT (file sort3.in)
9221333231
OUTPUT FORMAT
A single line containing the number of exchanges requiredSAMPLE OUTPUT (file sort3.out)
4
刚开始以为是选择排序 WA了
先把3放在它应该在的位置 并且交换的时候保证1在2的前面 同样再把2换到它该在的位置
/* ID: your_id_here PROG: sort3 LANG: C++*/#include <iostream>#include<cstdio>#include<cstring>#include<stdlib.h>#include<algorithm>using namespace std;int nu[1010],y[1010];int main(){ freopen("sort3.in","r",stdin); freopen("sort3.out","w",stdout); int i,j,k,n,s=0,x[5]={0},g=1,t; cin>>n; for(i = 1; i <= n ; i++) { cin>>nu[i]; x[nu[i]]++; } x[2]+=x[1]; x[3]+=x[2]; for(i = 1; i <= x[2] ; i++) { if(nu[i]==3) { s++; k = i; for(j = x[2]+1 ; j <= x[3] ; j++) if(nu[j]==1) { k = j; break; } if(k==i) { for(j = x[2]+1 ; j <= x[3] ; j++) if(nu[j]==2) { k = j; break; } } t = nu[i]; nu[i] = nu[k]; nu[k] = t; } } for(i = 1; i <= x[1] ; i++) { if(nu[i]==2) s++; } cout<<s<<endl; fclose(stdin); fclose(stdout); return 0;}
- usaco2.12Sorting a Three-Valued Sequence
- usaco2.1.3 Sorting a Three-Valued Sequence
- USACO2.1.3 Sorting A Three-Valued Sequence (sort3)
- USACO2.1.3 Sorting A Three-Valued Sequence (sort3)
- Sorting A Three-Valued Sequence
- Sorting a Three-Valued Sequence
- Sorting a Three-Valued Sequence
- Sorting A Three-Valued Sequence
- Sorting a Three-Valued Sequence
- Sorting a Three-Valued Sequence
- USACO2.1 三值的排序 Sorting a Three-Valued Sequence
- usaco2.1.3-----Sorting a Three-Valued Sequence(*三值交换排序)
- [USACO2.1]三值的排序 Sorting a Three-Valued Sequence
- 2.1.4 Sorting A Three-Valued Sequence
- Section 2.1 Sorting A Three-Valued Sequence
- 2.1Sorting A Three-Valued Sequence
- usaco training-Sorting a Three-Valued Sequence
- usaco: Sorting a Three-Valued Sequence
- (转)Android之父深入解析Android
- DICOM MPPS 的原理及应用(Principle and Application of DICOM MPPS)
- 移动客户端是否需要自动化测试
- delphi 实现 邮件发送(TidSMTP控件)
- ASP.NET中Global.asax全局应用程序类
- usaco2.12Sorting a Three-Valued Sequence
- Android.mk的用法和基础
- zImage内核镜像解压过程详解
- 区分Activity的四种加载模式
- PACKET套接字在用户态实现跨OS跨协议的NAT
- 两种方法搭建IA-32汇编设计环境
- 丰田汽车计划在2013年为其丰田和雷克萨斯汽车品牌生产879万辆汽车
- 实现广义表的相关运算算法(一)
- 新手箴言