USACO 2.1 Sorting A Three-Valued Sequence (sort3)
来源:互联网 发布:coc兵种升级数据2017 编辑:程序博客网 时间:2024/06/04 18:45
//Main Idear//The problem is quite easy, the key point is to consider all kinds of situation.//We first calculate the number of 1,2,3,namely, counter1,counter2,counter3//So, when the sequence is in non-decreasing order, number 1 shoud be in place(start from 0) //[0,counter1-1],2 in [counter1,counter1+counter2-1] and 3 in [counter1+counter2,sequence_lenght-1]//Then we can count then number in wrong place and use the min swap step to put them in order./*ID: haolink1PROG: sort3LANG: C++*///#include <iostream>#include <fstream>using namespace std;int main(){ ifstream fin("sort3.in"); short len = 0; fin >> len; short* numbers = new short[len]; for(short i = 0; i < len; i++){ fin >> numbers[i]; } //calculate the number of 1,2,3 and store them in counter[0],counter[1],counter[2] respectively short counter[3] = {0}; for(short i = 0; i < len; i++){ counter[numbers[i]-1]++; }// for(short i = 0; i < 3; i++){// cout<<counter[i]<<endl;// } //caltulate the number in the wrong place. //Eg: number 2, and 3 shoudn't appear in the palce that's lower then counter[0] short num_2_in_1 = 0; short num_3_in_1 = 0; for(short i = 0; i< counter[0]; i++){ if(numbers[i] == 2) num_2_in_1++; if(numbers[i] == 3) num_3_in_1++; } short num_1_in_2 = 0; short num_3_in_2 = 0; for(short i = counter[0]; i < counter[0]+counter[1]; i++){ if(numbers[i] == 1) num_1_in_2++; if(numbers[i] == 3) num_3_in_2++; } short num_1_in_3 = 0; for(short i =counter[0]+counter[1]; i < len; i++){ if(numbers[i] == 1) num_1_in_3++; }// cout<<num_2_in_1<<" "<<num_3_in_1<<" "<<num_1_in_2<<" "<<num_3_in_2<<" "<<num_1_in_3<<endl; //calculate the swap times we need to make the sequence in order. short swap_num = 0; if(num_2_in_1 > 0){ if(num_2_in_1 <= num_1_in_2){ swap_num += num_2_in_1; num_1_in_2 -= num_2_in_1; num_2_in_1 = 0; }else{ swap_num += num_1_in_2; num_2_in_1 -= num_1_in_2; num_1_in_2 = 0; } if(num_2_in_1 > 0){ swap_num += 2*num_2_in_1; num_1_in_3 -= num_2_in_1; num_3_in_2 -= num_2_in_1; num_2_in_1 = 0; } } if(num_3_in_1 > 0){ if(num_3_in_1 <= num_1_in_3){ swap_num += num_3_in_1; num_1_in_3 -= num_3_in_1; num_3_in_1 = 0; }else{ swap_num += num_1_in_3; num_3_in_1 -= num_1_in_3; num_1_in_3 = 0; } if(num_3_in_1 > 0){ swap_num += 2*num_3_in_1; num_1_in_2 -= num_3_in_1; //num_2_in_3 -= num_3_in_1; num_3_in_1 = 0; } } if(num_3_in_2 > 0){ swap_num += num_3_in_2; } ofstream fout("sort3.out"); fout<<swap_num<<endl; delete[] numbers; return 0;}
0 1
- USACO Sorting A Three-Valued Sequence (sort3)
- USACO 2.1 Sorting A Three-Valued Sequence (sort3)
- 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
- USACO2.1.3 Sorting A Three-Valued Sequence (sort3)
- USACO2.1.3 Sorting A Three-Valued Sequence (sort3)
- usaco training-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】【贪心】Sorting a Three-Valued Sequence
- USACO Sorting a Three-Valued Sequence
- C语言的那些秘密之---函数返回局部变量
- android打包apk时,注意不要混淆第三方类库,否则在运行时会报异常
- VC6.0(MFC)致命错误 无法打开包含文件 'afxres.h'.解决方案
- USACO 2.1 Ordered Fractions (frac1)
- 【c++】PAT (Advanced Level) 1005. Spell It Right (20)
- USACO 2.1 Sorting A Three-Valued Sequence (sort3)
- javase 利用随机数组成的猜数字游戏
- [C++]C++的一些小程序
- 蓝桥题目:特殊回文数
- USACO 2.1 Healthy Holsteins (holstein)
- 查找练习
- SSO——单点登陆原理与比较
- 【安卓笔记】盘点android中的dialog
- 从myeclipse导入eclipse导致不能识别为web项目