pku1804 Brainman

就是求个逆序对,基础编程能力还是要有的,交这个题时很囧。

PKU也有不给力的时候啊!

View Code
 1 program merge_sort(input,output);
2 var
3 a,x : array[0..2000] of longint;
4 n,answer : longint;
5 cases,v : longint;
6 procedure init;
7 var
8 i : longint;
9 begin
10 read(n);
11 for i:=1 to n do
12 read(x[i]);
13 readln;
14 answer:=0;
15 end; { init }
16 procedure merge(p,q : longint);
17 var
18 mid,i,j,pos : longint;
19 begin
20 if p>=q then exit;
21 mid:=(p+q)>>1;
22 merge(p,mid);
23 merge(mid+1,q);
24 i:=p;
25 j:=mid+1;
26 pos:=p;
27 while (i<=mid)and(j<=q) do
28 begin
29 if x[i]>x[j] then
30 begin
31 a[pos]:=x[j];
32 inc(answer,mid-i+1);
33 inc(pos);
34 inc(j);
35 continue;
36 end
37 else
38 begin
39 a[pos]:=x[i];
40 inc(pos);
41 inc(i);
42 continue;
43 end;
44 end;
45 while i<=mid do
46 begin
47 a[pos]:=x[i];
48 inc(pos);
49 inc(i);
50 end;
51 while j<=q do
52 begin
53 a[pos]:=x[j];
54 inc(pos);
55 inc(j);
56 end;
57 for i:=p to q do
58 x[i]:=a[i];
59 end; { merge }
60 procedure print;
61 begin
62 writeln('Scenario #',v,':');
63 writeln(answer);
64 writeln;
65 end; { print }
66 begin
67 readln(cases);
68 for v:=1 to cases do
69 begin
70 init;
71 merge(1,n);
72 print;
73 end;
74 end.



posted @ 2012-03-17 11:57  Codinginging  阅读(245)  评论(0编辑  收藏  举报