NOIP2010代码

一.机器翻译

View Code
 1 program translate(input,output);
2 var
3 q : array[0..10001] of longint;
4 v : array[0..1000] of boolean;
5 head,tail : longint;
6 n,m,ans,i,x : longint;
7 begin
8 assign(input,'translate.in');reset(input);
9 assign(output,'translate.out');rewrite(output);
10 fillchar(v,sizeof(v),false);
11 readln(m,n);
12 ans:=0;
13 head:=0;
14 tail:=0;
15 for i:=1 to n do
16 begin
17 read(x);
18 if not v[x] then
19 begin
20 v[x]:=true;
21 if tail-head<m then
22 begin
23 inc(ans);
24 inc(tail);
25 q[tail]:=x;
26 end
27 else
28 begin
29 inc(ans);
30 inc(head);
31 v[q[head]]:=false;
32 inc(tail);
33 q[tail]:=x;
34 end;
35 end
36 else
37 continue;
38 end;
39 writeln(ans);
40 close(input);
41 close(output);
42 end.

二.乌龟棋

View Code
 1 program tortoise(input,output);
2 var
3 f : array[-2..40,-2..40,-2..40,-2..40] of longint;
4 a : array[1..500] of longint;
5 num : array[1..4] of longint;
6 x,ans : longint;
7 n,m : longint;
8 procedure init;
9 var
10 i : longint;
11 begin
12 readln(n,m);
13 for i:=1 to n do
14 read(a[i]);
15 readln;
16 for i:=1 to m do
17 begin
18 read(x);
19 inc(num[x]);
20 end;
21 end; { init }
22 function max(aa,bb,cc,dd :longint ):longint;
23 var
24 t : longint;
25 begin
26 t:=aa;
27 if bb>t then
28 t:=bb;
29 if cc>t then
30 t:=cc;
31 if dd>t then
32 t:=dd;
33 exit(t);
34 end; { max }
35 procedure main;
36 var
37 i,j,k,l : longint;
38 begin
39 f[0,0,0,0]:=a[1];
40 for i:=0 to num[1] do
41 for j:=0 to num[2] do
42 for k:=0 to num[3] do
43 for l:=0 to num[4] do
44 f[i,j,k,l]:=max(f[i-1,j,k,l],f[i,j-1,k,l],f[i,j,k-1,l],f[i,j,k,l-1])+a[i*1+j*2+k*3+l*4+1];
45 writeln(f[num[1],num[2],num[3],num[4]]);
46 end; { main }
47 begin
48 assign(input,'tortoise.in');reset(input);
49 assign(output,'tortoise.out');rewrite(output);
50 init;
51 main;
52 close(input);
53 close(output);
54 end.

三.关押罪犯

View Code
  1 program prisons(input,output);
2 type
3 node = ^link;
4 link = record
5 goal : longint;
6 next : node;
7 end;
8 var
9 l : array[0..30000] of node;
10 x,y,w : array[0..220000] of longint;
11 colour : array[0..30000] of longint;
12 q : array[0..30000] of longint;
13 n,m,ans : longint;
14 procedure init;
15 var
16 i : longint;
17 begin
18 readln(n,m);
19 for i:=1 to m do
20 readln(x[i],y[i],w[i]);
21 end; { init }
22 procedure swap(var aa,bb :longint );
23 var
24 tt : longint;
25 begin
26 tt:=aa;
27 aa:=bb;
28 bb:=tt;
29 end; { swap }
30 procedure sort(p,q :longint );
31 var
32 i,j,mid : longint;
33 begin
34 i:=p;
35 j:=q;
36 mid:=w[(i+j)>>1];
37 repeat
38 while w[i]<mid do
39 inc(i);
40 while w[j]>mid do
41 dec(j);
42 if i<=j then
43 begin
44 swap(x[i],x[j]);
45 swap(y[i],y[j]);
46 swap(w[i],w[j]);
47 inc(i);
48 dec(j);
49 end;
50 until i>j;
51 if i<q then sort(i,q);
52 if j>p then sort(p,j);
53 end; { sort }
54 function bfs(now :longint ):boolean;
55 var
56 head,tail : longint;
57 t : node;
58 begin
59 new(t);
60 head:=0;
61 tail:=1;
62 colour[now]:=1;
63 q[1]:=now;
64 while head<tail do
65 begin
66 inc(head);
67 t:=l[q[head]];
68 while t<>nil do
69 begin
70 if colour[t^.goal]=0 then
71 begin
72 colour[t^.goal]:=3-colour[q[head]];
73 inc(tail);
74 q[tail]:=t^.goal;
75 end
76 else
77 if colour[t^.goal]=colour[q[head]] then
78 exit(false);
79 t:=t^.next;
80 end;
81 end;
82 exit(true);
83 end; { bfs }
84 function check():boolean;
85 var
86 i : longint;
87 begin
88 for i:=1 to n do
89 if colour[i]=0 then
90 if not bfs(i) then
91 exit(false);
92 exit(true);
93 end; { check }
94 procedure clean;
95 var
96 i : longint;
97 begin
98 fillchar(colour,sizeof(colour),0);
99 for i:=1 to n do
100 l[i]:=nil;
101 end; { clean }
102 procedure add(xx,yy :longint );
103 var
104 tt : node;
105 begin
106 new(tt);
107 tt^.goal:=yy;
108 tt^.next:=l[xx];
109 l[xx]:=tt;
110 end; { add }
111 procedure main;
112 var
113 l,r,mid,k : longint;
114 begin
115 l:=0;
116 r:=m;
117 while l<r do
118 begin
119 mid:=(l+r)>>1;
120 clean;
121 for k:=m downto mid do
122 begin
123 add(x[k],y[k]);
124 add(y[k],x[k]);
125 end;
126 if check() then
127 r:=mid
128 else
129 l:=mid+1;
130 end;
131 ans:=w[l-1];
132 end; { main }
133 procedure print;
134 begin
135 writeln(ans);
136 end; { print }
137 begin
138 assign(input,'prison.in');reset(input);
139 assign(output,'prison.out');rewrite(output);
140 init;
141 sort(1,m);
142 main;
143 print;
144 close(input);
145 close(output);
146 end.

四.引水入城

View Code
  1 {$M 100000000}
2 program flow(input,output);
3 type
4 node = record
5 fx,fy : integer;
6 end;
7 var
8 a : array[0..501,0..501] of longint;
9 v : array[0..501,0..501] of boolean;
10 f : array[0..501] of longint;
11 min,max : array[0..501] of longint;
12 m,n : longint;
13 q : array[0..250001] of node;
14 xi : array[1..4] of longint=(1,0,-1,0);
15 yi : array[1..4] of longint=(0,1,0,-1);
16 procedure clean(now: longint );
17 var
18 i : longint;
19 begin
20 for i:=0 to n+1 do
21 begin
22 a[i,0]:=now;
23 a[i,m+1]:=now;
24 end;
25 for i:=0 to m+1 do
26 begin
27 a[0,i]:=now;
28 a[n+1,i]:=now;
29 end;
30 end; { clean }
31 procedure init;
32 var
33 i,j : longint;
34 begin
35 for i:=0 to 501 do
36 for j:=0 to 501 do
37 a[i,j]:=-1100;
38 readln(n,m);
39 for i:=1 to n do
40 for j:=1 to m do
41 read(a[i,j]);
42 fillchar(v,sizeof(v),false);
43 fillchar(min,sizeof(min),63);
44 fillchar(max,sizeof(max),0);
45 end; { init }
46 procedure floodfill(xx,yy :longint );
47 var
48 newx,newy : longint;
49 i : longint;
50 begin
51 v[xx,yy]:=true;
52 for i:=1 to 4 do
53 begin
54 newx:=xx+xi[i];
55 newy:=yy+yi[i];
56 if not v[newx,newy] then
57 if a[xx,yy]>a[newx,newy] then
58 floodfill(newx,newy);
59 end;
60 end; { floodfill }
61 procedure Nosolution;
62 var
63 i,ans : longint;
64 flag : boolean;
65 begin
66 clean(maxlongint>>2);
67 for i:=1 to m do
68 if not v[1,i] then
69 floodfill(1,i);
70 flag:=true;
71 for i:=1 to m do
72 if not v[n,i] then
73 flag:=false;
74 if flag then
75 exit;
76 ans:=0;
77 for i:=1 to m do
78 if not v[n,i] then
79 inc(ans);
80 writeln('0');
81 writeln(ans);
82 close(input);
83 close(output);
84 halt;
85 end; { Nosolution }
86 procedure bfsmin(xx,yy :integer );
87 var
88 newx,newy : longint;
89 head,tail,i : longint;
90 begin
91 head:=0;
92 tail:=1;
93 q[1].fx:=xx;
94 q[1].fy:=yy;
95 v[xx,yy]:=true;
96 while head<tail do
97 begin
98 inc(head);
99 for i:=1 to 4 do
100 begin
101 newx:=q[head].fx+xi[i];
102 newy:=q[head].fy+yi[i];
103 if a[newx,newy]>a[q[head].fx,q[head].fy] then
104 if not v[newx,newy] then
105 begin
106 inc(tail);
107 v[newx,newy]:=true;
108 q[tail].fx:=newx;
109 q[tail].fy:=newy;
110 if newx=1 then
111 min[newy]:=yy;
112 end;
113 end;
114 end;
115 end; { bfsmin }
116 procedure bfsmax(xx,yy :integer );
117 var
118 newx,newy : longint;
119 head,tail,i : longint;
120 begin
121 head:=0;
122 tail:=1;
123 q[1].fx:=xx;
124 q[1].fy:=yy;
125 v[xx,yy]:=true;
126 while head<tail do
127 begin
128 inc(head);
129 for i:=1 to 4 do
130 begin
131 newx:=q[head].fx+xi[i];
132 newy:=q[head].fy+yi[i];
133 if a[newx,newy]>a[q[head].fx,q[head].fy] then
134 if not v[newx,newy] then
135 begin
136 inc(tail);
137 q[tail].fx:=newx;
138 q[tail].fy:=newy;
139 v[newx,newy]:=true;
140 if newx=1 then
141 max[newy]:=yy;
142 end;
143 end;
144 end;
145 end; { bfsmax }
146 procedure previous;
147 var
148 i : longint;
149 begin
150 clean(-1000);
151 fillchar(v,sizeof(v),false);
152 for i:=1 to m do
153 BfsMin(n,i);
154 clean(-1000);
155 fillchar(v,sizeof(v),false);
156 for i:=m downto 1 do
157 BfsMax(n,i);
158 end; { previous }
159 procedure try;
160 var
161 i : longint;
162 begin
163 for i:=1 to m do
164 writeln(min[i],'',max[i]);
165 end; { try }
166 procedure finally;
167 var
168 i,j : longint;
169 begin
170 if n=1 then
171 for i:=1 to m do
172 begin
173 if (min[i]>1995) then
174 min[i]:=i;
175 if (max[i]=0) then
176 max[i]:=i;
177 end;
178 fillchar(f,sizeof(f),63);
179 f[0]:=0;
180 for i:=1 to m do
181 if (max[i]>0)and(min[i]<9950714) then
182 for j:=max[i] downto min[i]-1 do
183 if f[j]+1<f[max[i]] then
184 f[max[i]]:=f[j]+1;
185 end; { finally }
186 procedure print;
187 begin
188 writeln('1');
189 writeln(f[m]);
190 end; { print }
191 begin
192 assign(input,'flow.in');reset(input);
193 assign(output,'flow.out');rewrite(output);
194 init;
195 Nosolution;
196 previous;
197 finally;
198 print;
199 close(input);
200 close(output);
201 end.

这就是我的所有代码了,不懂就留言,但事实上根据变量,过程名称就能知道是做什么的!

posted @ 2011-10-13 07:10  Codinginging  阅读(343)  评论(0编辑  收藏  举报