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 }

posted @ 2011-05-03 13:12  幻魇  阅读(206)  评论(0编辑  收藏  举报