USACO Sorting a Three-Valued Sequence
来源:互联网 发布:网络好歌推荐100首 编辑:程序博客网 时间:2024/05/21 23:32
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 mostthree 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
/*ID: qhn9992PROG: sort3LANG: C++11*/#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int a[1100],n,b[1100],dp[10][10],ans;int main(){ freopen("sort3.in","r",stdin); freopen("sort3.out","w",stdout); scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d",a+i),b[i]=a[i]; sort(b,b+n); for(int i=0;i<n;i++) dp[b[i]][a[i]]++; ///1<--->2 int temp; temp=min(dp[1][2],dp[2][1]); ans+=temp; dp[1][2]-=temp;dp[2][1]-=temp; ///1<--->3 temp=min(dp[1][3],dp[3][1]); ans+=temp; dp[1][3]-=temp;dp[3][1]-=temp; ///2<--->3 temp=min(dp[2][3],dp[3][2]); ans+=temp; dp[2][3]-=temp;dp[3][2]-=temp; ans+=(dp[1][2]+dp[1][3])*2; printf("%d\n",ans); return 0;}
- usaco training-Sorting a Three-Valued Sequence
- usaco: Sorting a Three-Valued Sequence
- USACO Sorting A Three-Valued Sequence (sort3)
- usaco Sorting a Three-Valued Sequence
- USACO-Sorting a Three-Valued Sequence
- USACO Sorting a Three-Valued Sequence
- USACO 2.1 Sorting a Three-Valued Sequence
- USACO 2.1 Sorting a Three-Valued Sequence
- USACO--2.1Sorting a Three-Valued Sequence
- USACO 2.1 Sorting A Three-Valued Sequence
- USACO [2.1] Sorting a Three-Valued Sequence
- USACO 2.1-Sorting a Three-Valued Sequence
- USACO 2.1 Sorting a Three-Valued Sequence
- 【USACO】【贪心】Sorting a Three-Valued Sequence
- USACO Sorting a Three-Valued Sequence
- USACO :Sorting a Three-Valued Sequence解题报告
- USACO 2.1.3 Sorting A Three-Valued Sequence
- [USACO 2.1.3] Sorting a Three-Valued Sequence
- 对于Android来说除了SDK和NDK外,ADK被称为第三大革命型
- Oracle MySQL 语法上的不同
- 解方程组
- WEB之回发页面
- Subsets
- USACO Sorting a Three-Valued Sequence
- HreoWinGauge2.0组件集应用示例----数据输入输出
- 在mybatis中遇到奇怪的问题
- Mac OS 10.9.1 输入法切换(Vmware)
- 设计模式之简单工厂模式
- Jump Game II
- 【C++学习】vector的使用,输入一串数字,输出相邻和---ShinePans
- Displaytag实现分页
- 杭电 2095