USACO 2.1 Sorting A Three-Valued Sequence

TASK: sort3
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3028 KB]
   Test 2: TEST OK [0.000 secs, 3028 KB]
   Test 3: TEST OK [0.000 secs, 3028 KB]
   Test 4: TEST OK [0.000 secs, 3028 KB]
   Test 5: TEST OK [0.000 secs, 3028 KB]
   Test 6: TEST OK [0.000 secs, 3028 KB]
   Test 7: TEST OK [0.000 secs, 3028 KB]
   Test 8: TEST OK [0.000 secs, 3028 KB]

All tests OK.

Your program ('sort3') produced all correct answers! This is your submission #3 for this problem. Congratulations!

1 /*
2 ID: jiafeim1
3 PROG: sort3
4 LANG: C++
5 */
6 #include <iostream>
7 #include <fstream>
8 #include <algorithm>
9 using namespace std;
10
11 #define maxN(x,y) ((x)>(y)?(x):(y))
12
13 int num[1003];
14 int sorted[1003];
15 int countX[4][4]={0};
16 int main()
17 {
18 ofstream fout ("sort3.out");
19 ifstream fin ("sort3.in");
20
21 int n;
22 fin>>n;
23
24 for(int i=0;i!=n;++i)
25 {
26 fin>>num[i];
27 sorted[i] = num[i];
28 }
29
30 sort(sorted,sorted+n);
31
32 int total = 0;
33 for(int i = 0;i!=n;++i)
34 {
35 if(sorted[i]!=num[i])
36 ++countX[sorted[i]][num[i]];
37 }
38 total =countX[2][1]+countX[3][1]+countX[2][3]+max(0,countX[1][3]-countX[3][1]);
39
40 fout<<total<<endl;
41 fin.close();
42 fout.close();
43 return 0;
44 }

转载于:https://www.cnblogs.com/huanyan/archive/2011/05/03/USACO_Sorting_A_Three-Valued_Sequence.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值