USACO 2.1 Sorting a Three-Valued Sequence
来源:互联网 发布:网络彩票恢复最新消息 编辑:程序博客网 时间:2024/05/21 15:00
题目:
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 threedifferent 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}/*ID: gjj50201LANG: C++TASK: sort3*/#include<stdio.h>#include<iostream>#include<string>#include<stdlib.h>#include<algorithm>using namespace std;int a[1001];int main(){freopen("sort3.in","r",stdin);freopen("sort3.out","w",stdout);int n,n1 = 0,n2 = 0,ans = 0,j;cin>>n;for(int i=1; i<=n; i++){cin>>a[i];if(a[i] == 1) n1++;else if(a[i] == 2) n2++;}for(int i=1;i<=n1;i++){if(a[i] == 2){//cout<<i<<endl;for(j=n1+1;j<=n;j++)if(a[j] == 1)break;swap(a[i],a[j]);ans++;}else if(a[i]==3){for(j=n;j>=n1+1;j--)if(a[j] == 1)break;swap(a[i],a[j]);ans++;}}// for(int i=1;i<=n;i++)// cout<<a[i]<<" ";// cout<<endl;for(int i=n1+1; i<=n1+n2; i++)if(a[i] == 3)ans ++;cout<<ans<<endl;return 0;}
- 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 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】【贪心】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
- USACO section 2.1 Sorting a Three-Valued Sequence
- 近期开发需要用的service案例 ssh+jdbc
- FlyoutMenus
- 2015华为校招机试题
- ViewPagerCards
- 第七章 浮动
- USACO 2.1 Sorting a Three-Valued Sequence
- 【codeforces 723C C. Sanatorium】
- 线程同步
- Configuration problem: Failed to import bean definitions from relative location
- 可行性研究
- Spring Boot学习笔记
- RelativePopupWindow
- Drupal学习笔记一:Druple安装
- 自定义滚动条案例