[NOIP2015]代码

今年真是挂惨了。。。。。。

考场:

T1 magic AAAAAAAAAA

 1 program _magic;
 2 var
 3     n:integer;
 4     a:array[1..39,1..39] of integer;
 5     i,j,ls1,ls2:integer;
 6 begin
 7     assign(input,'magic.in');
 8     reset(input);
 9     assign(output,'magic.out');
10     rewrite(output);
11     readln(n);
12     fillchar(a,sizeof(a),0);
13     ls1:=1;
14     ls2:=(n+1) div 2;
15     a[ls1,ls2]:=1;
16     for i:=2 to n*n do
17     begin
18         if (ls1=1)and(ls2<n) then
19         begin
20             ls1:=n;
21             ls2:=ls2+1;
22         end
23         else
24         if (ls2=n)and(ls1>1) then
25         begin
26             ls1:=ls1-1;
27             ls2:=1;
28         end
29         else if(ls1=1)and(ls2=n) then
30         begin
31             ls1:=ls1+1;
32             ls2:=ls2;
33         end
34         else if (ls1>1)and(ls2<n) then
35         begin
36             if a[ls1-1,ls2+1]=0 then
37             begin
38                 ls1:=ls1-1;
39                 ls2:=ls2+1;
40             end
41             else
42             begin
43                 ls1:=ls1+1;
44                 ls2:=ls2;
45             end;
46         end;
47         a[ls1,ls2]:=i;
48     end;
49     for i:=1 to n do
50     begin
51         for j:=1 to n-1 do
52             write(a[i,j],' ');
53         writeln(a[i,n]);
54     end;
55     close(input);
56     close(output);
57 end.
magic.pas

 T2 message AAAAAAAAAA

 1 program _message;
 2 var
 3     n:longint;
 4     t:array[1..200020] of longint;
 5     i,j:longint;
 6     ord:array[1..200020] of longint;
 7     vis:array[1..200020] of boolean;
 8     ans:longint;
 9 procedure check(x:longint);
10 var
11     i,j,k:longint;
12 begin
13     i:=x;
14     j:=0;
15     while not(vis[i]) do
16     begin
17          inc(j);
18          ord[j]:=i;
19          vis[i]:=true;
20          i:=t[i];
21     end;
22     k:=1;
23     while (k<=j)and(ord[k]<>i) do
24         inc(k);
25     if k<=j then
26     begin
27         if ans>j+1-k then
28             ans:=j+1-k;
29     end;
30 end;
31 begin
32     assign(input,'message.in');
33     reset(input);
34     assign(output,'message.out');
35     rewrite(output);
36     readln(n);
37     ans:=maxlongint;
38     for i:=1 to n do
39         read(t[i]);
40     fillchar(vis,sizeof(vis),0);
41     for i:=1 to n do
42         if not(vis[i]) then
43             check(i);
44     writeln(ans);
45     close(input);
46     close(output);
47 end.
message.pas

 T3 landlords WWWWWWWWWWWWWWWWWWWT

  1 program _landlords;
  2 type
  3     arr=array[1..23] of boolean;
  4     card=record
  5         a,b:integer;
  6     end;
  7 var
  8     t,n:integer;
  9     i,j,q,s,d:integer;
 10     k,p:longint;
 11     l,r:longint;
 12     cards:array[1..23] of card;
 13     queue:array[0..10000000] of longint;
 14     len:array[0..10000000] of integer;
 15     vis:array[0..10000000] of boolean;
 16     ls1,ls2:arr;
 17 function less(p,q:card):boolean;
 18 begin
 19     if p.a=0 then
 20     begin
 21         if q.a=0 then
 22             exit(p.b<q.b)
 23         else
 24             exit(true);
 25     end
 26     else
 27     begin
 28         if q.a=0 then
 29             exit(false);
 30         if p.b<q.b then
 31             exit(true);
 32         if p.b>q.b then
 33             exit(false);
 34         exit(p.a<q.a);
 35     end;
 36 end;
 37 procedure qsort(l,r:integer);
 38 var
 39     i,j:integer;
 40     x,y:card;
 41 begin
 42     i:=l;
 43     j:=r;
 44     x:=cards[(l+r) div 2];
 45     repeat
 46         while less(cards[i],x) do
 47             inc(i);
 48         while less(x,cards[j]) do
 49             dec(j);
 50         if i<=j then
 51         begin
 52             y:=cards[i];
 53             cards[i]:=cards[j];
 54             cards[j]:=y;
 55             inc(i);
 56             dec(j);
 57         end;
 58     until i>j;
 59     if l<j then
 60         qsort(l,j);
 61     if i<r then
 62         qsort(i,r);
 63 end;
 64 function hash(p:arr):longint;
 65 var
 66     i:integer;
 67     ans:longint;
 68 begin
 69     ans:=0;
 70     for i:=n downto 1 do
 71         ans:=ans*2+ord(p[i]);
 72     exit(ans);
 73 end;
 74 function inhash(p:longint):arr;
 75 var
 76     i:integer;
 77     ans:arr;
 78 begin
 79     i:=1;
 80     fillchar(ans,sizeof(ans),0);
 81     while p>0 do
 82     begin
 83         ans[i]:=((p mod 2)=1);
 84         p:=p div 2;
 85         inc(i);
 86     end;
 87     exit(ans);
 88 end;
 89 procedure clear;
 90 begin
 91     l:=1;
 92     r:=0;
 93 end;
 94 procedure inque(x:longint);
 95 begin
 96     inc(r);
 97     queue[r]:=x;
 98     vis[x]:=true;
 99 end;
100 procedure deque;
101 begin
102     inc(l);
103 end;
104 function sum(x:integer):integer;
105 var
106     i,t:integer;
107 begin
108     t:=0;
109     for i:=1 to n do
110         if (cards[i].a=x)and(ls1[x]) then
111             t:=t+1;
112     exit(t);
113 end;
114 function find(p,x:integer):integer;
115 var
116     i:integer;
117 begin
118     for i:=p+1 to x do
119         if (cards[i].a=x)and(ls1[i]) then
120             exit(i);
121 end;
122 begin
123     assign(input,'landlords.in');
124     reset(input);
125     assign(output,'landlords.out');
126     rewrite(output);
127     readln(t,n);
128     for i:=1 to t do
129     begin
130         for j:=1 to n do
131             read(cards[j].a,cards[j].b);
132         qsort(1,n);
133         clear;
134         fillchar(ls1,sizeof(ls1),1);
135         for k:=0 to hash(ls1) do
136         begin
137             vis[k]:=false;
138             len[k]:=10000;
139         end;
140         inque(hash(ls1));
141         len[hash(ls1)]:=0;
142         while l<=r do
143         begin
144             ls1:=inhash(queue[l]);
145             if hash(ls1)=0 then
146             begin
147                 writeln(len[0]);
148                 close(input);
149                 close(output);
150                 halt;
151             end;
152             deque;
153             for q:=1 to n do
154                 if ls1[q] then
155                     break;
156             if cards[q].a=0 then
157             begin
158                 ls2:=ls1;
159                 ls2[q]:=false;
160                 if not(vis[hash(ls2)]) then
161                 begin
162                     inque(hash(ls2));
163                     len[hash(ls2)]:=len[hash(ls1)]+1;
164                 end;
165                 if (ls1[1])and(ls1[2])and(cards[1].a=0)and(cards[2].a=0) then
166                 begin
167                     ls2:=ls1;
168                     ls2[1]:=false;
169                     ls2[2]:=false;
170                     if not(vis[hash(ls2)]) then
171                     begin
172                         inque(hash(ls2));
173                         len[hash(ls2)]:=len[hash(ls1)]+1;
174                     end;
175                 end;
176             end
177             else
178             begin
179                     if sum(cards[q].a)>=4 then
180                     begin
181                         ls2:=ls1;
182                         for s:=q to q+3 do
183                             ls2[s]:=false;
184                         if not(vis[hash(ls2)]) then
185                         begin
186                             inque(hash(ls2));
187                             len[hash(ls2)]:=len[hash(ls1)]+1;
188                         end;
189                         for s:=1 to n do
190                             for d:=1 to n do
191                             begin
192                                 if (cards[s].a=cards[q].a)or(cards[d].a=cards[q].a) then
193                                     continue;
194                                 if (not(ls1[s]))or(not(ls1[t])) then
195                                     continue;
196                                 if s=d then
197                                     continue;
198                                 ls2:=ls1;
199                                 ls2[s]:=false;
200                                 ls2[d]:=false;
201                                 ls2[q]:=false;
202                                 ls2[q+1]:=false;
203                                 ls2[q+2]:=false;
204                                 ls2[q+3]:=false;
205                                 if not(vis[hash(ls2)]) then
206                                 begin
207                                     inque(hash(ls2));
208                                     len[hash(ls2)]:=len[hash(ls1)]+1;
209                                 end;
210                             end;
211                         for s:=1 to 13 do
212                             for t:=1 to 13 do
213                             begin
214                                 if s=t then
215                                 begin
216                                     if sum(s)<4 then
217                                         continue;
218                                     ls2:=ls1;
219                                     ls2[q]:=false;
220                                     ls2[q+1]:=false;
221                                     ls2[q+2]:=false;
222                                     ls2[q+3]:=false;
223                                     ls2[find(0,s)]:=false;
224                                     ls2[find(0,s)+1]:=false;
225                                     ls2[find(0,s)+2]:=false;
226                                     ls2[find(0,s)+3]:=false;
227                                     if not(vis[hash(ls2)]) then
228                                     begin
229                                         inque(hash(ls2));
230                                         len[hash(ls2)]:=len[hash(ls1)]+1;
231                                     end;
232                                 end;
233                                 if (s=cards[q].a)or(t=cards[q].a) then
234                                     continue;
235                                 if (sum(s)<2)or(sum(t)<2) then
236                                     continue;
237                                 ls2:=ls1;
238                                 ls2[q]:=false;
239                                 ls2[q+1]:=false;
240                                 ls2[q+2]:=false;
241                                 ls2[q+3]:=false;
242                                 ls2[find(0,s)]:=false;
243                                 ls2[find(find(0,s),s)]:=false;
244                                 ls2[find(0,t)]:=false;
245                                 ls2[find(find(0,t),t)]:=false;
246                                 if not(vis[hash(ls2)]) then
247                                 begin
248                                     inque(hash(ls2));
249                                     len[hash(ls2)]:=len[hash(ls1)]+1;
250                                 end;
251                             end;
252                     end;
253                     if sum(cards[q].a)>=3 then
254                     begin
255                         ls2:=ls1;
256                         for s:=q to q+2 do
257                             ls2[s]:=false;
258                         if not(vis[hash(ls2)]) then
259                         begin
260                             inque(hash(ls2));
261                             len[hash(ls2)]:=len[hash(ls1)]+1;
262                         end;
263                         for s:=1 to n do
264                         begin
265                             if cards[s].a=cards[q].a then
266                                 continue;
267                             if not(ls1[s]) then
268                                 continue;
269                             ls2:=ls1;
270                             ls2[s]:=false;
271                             ls2[q]:=false;
272                             ls2[q+1]:=false;
273                             ls2[q+2]:=false;
274                             if not(vis[hash(ls2)]) then
275                             begin
276                                 inque(hash(ls2));
277                                 len[hash(ls2)]:=len[hash(ls1)]+1;
278                             end;
279                         end;
280                         for s:=1 to 13 do
281                         begin
282                             if s=cards[q].a then
283                                 continue;
284                             if sum(s)<2 then
285                                 continue;
286                             ls2:=ls1;
287                             ls2[q]:=false;
288                             ls2[q+1]:=false;
289                             ls2[q+2]:=false;
290                             ls2[find(0,s)]:=false;
291                             ls2[find(find(0,s),s)]:=false;
292                             if not(vis[hash(ls2)]) then
293                             begin
294                                 inque(hash(ls2));
295                                 len[hash(ls2)]:=len[hash(ls1)]+1;
296                             end;
297                         end;
298                         s:=cards[q].a;
299                         while (s<=13)and(sum(s)>=3) do
300                             inc(s);
301                         if s<=13 then
302                         begin
303                             dec(s);
304                             if s-cards[q].a>=1 then
305                             begin
306                                 ls2:=ls1;
307                                 for d:=cards[q].a to s do
308                                 begin
309                                     ls2[find(0,d)]:=false;
310                                     ls2[find(find(0,d),d)]:=false;
311                                     ls2[find(find(find(0,d),d),d)]:=false;
312                                     if (d-cards[q].a>=1)and(not(vis[hash(ls2)])) then
313                                     begin
314                                         inque(hash(ls2));
315                                         len[hash(ls2)]:=len[hash(ls1)]+1;
316                                     end;
317                                 end;
318                             end;
319                         end else
320                         begin
321                             if sum(1)>=3 then
322                             begin
323                                 for d:=cards[q].a to 13 do
324                                 begin
325                                     ls2[find(0,d)]:=false;
326                                     ls2[find(find(0,d),d)]:=false;
327                                     ls2[find(find(find(0,d),d),d)]:=false;
328                                     if (d-cards[q].a>=1)and(not(vis[hash(ls2)])) then
329                                     begin
330                                         inque(hash(ls2));
331                                         len[hash(ls2)]:=len[hash(ls1)]+1;
332                                     end;
333                                 end;
334                                 ls2[find(0,1)]:=false;
335                                 ls2[find(find(0,1),1)]:=false;
336                                 ls2[find(find(find(0,1),1),1)]:=false;
337                                 if not(vis[hash(ls2)]) then
338                                 begin
339                                     inque(hash(ls2));
340                                     len[hash(ls2)]:=len[hash(ls1)]+1;
341                                 end;
342                             end;
343                         end;
344                     end;
345                     if sum(cards[q].a)>=2 then
346                     begin
347                         ls2:=ls1;
348                         for s:=q to q+1 do
349                             ls2[s]:=false;
350                         if not(vis[hash(ls2)]) then
351                         begin
352                             inque(hash(ls2));
353                             len[hash(ls2)]:=len[hash(ls1)]+1;
354                         end;
355                         for s:=1 to 13 do
356                         begin
357                             if s=cards[q].a then
358                                 continue;
359                             if sum(s)<3 then
360                                 continue;
361                             ls2:=ls1;
362                             ls2[q]:=false;
363                             ls2[q+1]:=false;
364                             ls2[find(0,s)]:=false;
365                             ls2[find(find(0,s),s)]:=false;
366                             ls2[find(find(find(0,s),s),s)]:=false;
367                             if not(vis[hash(ls2)]) then
368                             begin
369                                 inque(hash(ls2));
370                                 len[hash(ls2)]:=len[hash(ls1)]+1;
371                             end;
372                         end;
373                         s:=cards[q].a;
374                         while (s<=13)and(sum(s)>=2) do
375                             inc(s);
376                         if s<=13 then
377                         begin
378                             dec(s);
379                             if s-cards[q].a>=3 then
380                             begin
381                                 ls2:=ls1;
382                                 for d:=cards[q].a to s do
383                                 begin
384                                     ls2[find(0,d)]:=false;
385                                     ls2[find(find(0,d),d)]:=false;
386                                     if (d-cards[q].a>=2)and(not(vis[hash(ls2)])) then
387                                     begin
388                                         inque(hash(ls2));
389                                         len[hash(ls2)]:=len[hash(ls1)]+1;
390                                     end;
391                                 end;
392                             end;
393                         end else
394                         begin
395                             if (sum(1)>=2)and(13-cards[p].a+2>=3) then
396                             begin
397                                 for d:=cards[q].a to 13 do
398                                 begin
399                                     ls2[find(0,d)]:=false;
400                                     ls2[find(find(0,d),d)]:=false;
401                                     if (d-cards[q].a>=2)and(not(vis[hash(ls2)])) then
402                                     begin
403                                         inque(hash(ls2));
404                                         len[hash(ls2)]:=len[hash(ls1)]+1;
405                                     end;
406                                 end;
407                                 ls2[find(0,1)]:=false;
408                                 ls2[find(find(0,1),1)]:=false;
409                                 if not(vis[hash(ls2)]) then
410                                 begin
411                                     inque(hash(ls2));
412                                     len[hash(ls2)]:=len[hash(ls1)]+1;
413                                 end;
414                             end;
415                         end;
416                         for s:=1 to 13 do
417                             for t:=1 to 13 do
418                             begin
419                                 if s=t then
420                                 if (s=cards[q].a)or(t=cards[q].a) then
421                                     continue;
422                                 if (sum(s)<4)or(sum(t)<2) then
423                                     continue;
424                                 ls2:=ls1;
425                                 ls2[q]:=false;
426                                 ls2[q+1]:=false;
427                                 ls2[find(0,s)]:=false;
428                                 ls2[find(find(0,s),s)]:=false;
429                                 ls2[find(find(find(0,s),s),s)]:=false;
430                                 ls2[find(find(find(find(0,s),s),s),s)]:=false;
431                                 ls2[find(0,t)]:=false;
432                                 ls2[find(find(0,t),t)]:=false;
433                                 if not(vis[hash(ls2)]) then
434                                 begin
435                                     inque(hash(ls2));
436                                     len[hash(ls2)]:=len[hash(ls1)]+1;
437                                 end;
438                             end;
439                     end;
440                     if sum(cards[q].a)>=1 then
441                     begin
442                         ls2:=ls1;
443                         ls2[q]:=false;
444                         if not(vis[hash(ls2)]) then
445                         begin
446                             inque(hash(ls2));
447                             len[hash(ls2)]:=len[hash(ls1)]+1;
448                         end;
449                         for s:=1 to 13 do
450                         begin
451                             if s=cards[q].a then
452                                 continue;
453                             if sum(s)<3 then
454                                 continue;
455                             ls2:=ls1;
456                             ls2[q]:=false;
457                             ls2[find(0,s)]:=false;
458                             ls2[find(find(0,s),s)]:=false;
459                             ls2[find(find(find(0,s),s),s)]:=false;
460                             if not(vis[hash(ls2)]) then
461                             begin
462                                 inque(hash(ls2));
463                                 len[hash(ls2)]:=len[hash(ls1)]+1;
464                             end;
465                         end;
466                         s:=cards[q].a;
467                         while (s<=13)and(sum(s)>=1) do
468                             inc(s);
469                         if s<=13 then
470                         begin
471                             dec(s);
472                             if s-cards[q].a>=5 then
473                             begin
474                                 ls2:=ls1;
475                                 for d:=cards[q].a to s do
476                                 begin
477                                     ls2[find(0,d)]:=false;
478                                     if (d-cards[q].a>=5)and(not(vis[hash(ls2)])) then
479                                     begin
480                                         inque(hash(ls2));
481                                         len[hash(ls2)]:=len[hash(ls1)]+1;
482                                     end;
483                                 end;
484                             end;
485                         end else
486                         begin
487                             if (sum(1)>=1)and(13-cards[p].a+2>=5) then
488                             begin
489                                 for d:=cards[q].a to 13 do
490                                 begin
491                                     ls2[find(0,d)]:=false;
492                                     if (d-cards[q].a>=5)and(not(vis[hash(ls2)])) then
493                                     begin
494                                         inque(hash(ls2));
495                                         len[hash(ls2)]:=len[hash(ls1)]+1;
496                                     end;
497                                 end;
498                                 ls2[find(0,1)]:=false;
499                                 if not(vis[hash(ls2)]) then
500                                 begin
501                                     inque(hash(ls2));
502                                     len[hash(ls2)]:=len[hash(ls1)]+1;
503                                 end;
504                             end;
505                         end;
506                         for s:=1 to 13 do
507                             for t:=1 to 13 do
508                             begin
509                                 if s=t then
510                                 if (s=cards[q].a)or(t=cards[q].a) then
511                                     continue;
512                                 if (sum(s)<4)or(sum(t)<1) then
513                                     continue;
514                                 ls2:=ls1;
515                                 ls2[q]:=false;
516                                 ls2[find(0,s)]:=false;
517                                 ls2[find(find(0,s),s)]:=false;
518                                 ls2[find(find(find(0,s),s),s)]:=false;
519                                 ls2[find(find(find(find(0,s),s),s),s)]:=false;
520                                 ls2[find(0,t)]:=false;
521                                 if not(vis[hash(ls2)]) then
522                                 begin
523                                     inque(hash(ls2));
524                                     len[hash(ls2)]:=len[hash(ls1)]+1;
525                                 end;
526                             end;
527                     end;
528                 end;
529 
530         end;
531     end;
532     close(input);
533     close(output);
534 end.
landlords.pas

 T4 stone AAWWWAAAAW

  1 program _stone;
  2 type
  3     len=record
  4         l,r:longint;
  5         long:longint;
  6     end;
  7 var
  8     l,n,m:longint;
  9     dis:array[1..50050] of len;
 10     point:array[1..50050,0..2] of longint;
 11     pos:array[1..50050] of longint;
 12     i,j,k,j1,k1:longint;
 13     ans:longint;
 14 procedure makeheap(x:longint);
 15 var
 16     p,q:longint;
 17     ls:len;
 18 begin
 19     p:=x;
 20     while p*2<=n do
 21     begin
 22         if (dis[p*2+1].long<dis[p*2].long)and(p*2+1<=n) then
 23             q:=p*2+1
 24         else
 25             q:=p*2;
 26         if dis[p].long<=dis[q].long then
 27             break;
 28         pos[dis[p].l]:=q;
 29         pos[dis[q].l]:=p;
 30         ls:=dis[p];
 31         dis[p]:=dis[q];
 32         dis[q]:=ls;
 33         p:=q;
 34     end;
 35 end;
 36 procedure deltop;
 37 var
 38     ls:len;
 39 begin
 40     pos[dis[n].l]:=1;
 41     dis[1]:=dis[n];
 42     dec(n);
 43     makeheap(1);
 44 end;
 45 procedure change(x,y,l,r:longint);
 46 begin
 47     dis[x].long:=y;
 48     dis[x].l:=l;
 49     dis[x].r:=r;
 50     while x>=1 do
 51     begin
 52         makeheap(x);
 53         x:=x div 2;
 54     end;
 55 end;
 56 begin
 57     assign(input,'stone.in');
 58     reset(input);
 59     assign(output,'stone.out');
 60     rewrite(output);
 61     readln(l,n,m);
 62     pos[1]:=1;
 63     point[1,0]:=0;
 64     point[1,1]:=0;
 65     point[1,2]:=1;
 66     point[n+2,0]:=n-1;
 67     point[n+2,1]:=l;
 68     point[n+2,2]:=0;
 69     for i:=2 to n+1 do
 70     begin
 71         readln(point[i,1]);
 72         point[i,0]:=i-1;
 73         point[i,2]:=i+1;
 74         dis[i-1].l:=i-1;
 75         dis[i-1].r:=i;
 76         dis[i-1].long:=point[i,1]-point[i-1,1];
 77         pos[i-1]:=i-1;
 78     end;
 79     dis[n+1].l:=n+1;
 80     dis[n+1].r:=n+2;
 81     dis[n+1].long:=l-point[n+1,1];
 82     pos[n+1]:=n+1;
 83     n:=n+1;
 84     for i:=n downto 1 do
 85         makeheap(i);
 86     for i:=1 to m do
 87     begin
 88         j:=dis[1].l;
 89         k:=dis[1].r;
 90         j1:=point[j,0];
 91         k1:=point[k,2];
 92         deltop;
 93         if j1=0 then
 94             change(pos[k],point[k1,1]-point[j,1],j,k1)
 95         else if k1=0 then
 96             change(pos[j1],point[k,1]-point[j1,1],j1,k)
 97         else if (point[j,1]-point[j1,1]<point[k1,1]-point[k,1]) then
 98             change(pos[j1],point[k,1]-point[j1,1],j1,k)
 99         else
100             change(pos[k],point[k1,1]-point[j,1],j,k1);
101     end;
102     ans:=$3f3f3f3f;
103     for i:=1 to n do
104         if ans>dis[i].long then
105             ans:=dis[i].long;
106     writeln(ans);
107     close(input);
108     close(output);
109 end.
stone.pas

 T5 substring MMMMMMMMMM

 1 program substring;
 2 var
 3     n,m,k:longint;
 4     a,b:ansistring;
 5     ans:array[0..1000,0..200,0..200] of longint;
 6     i,j,p,q,r:longint;
 7 function min(x,y:longint):longint;
 8 begin
 9     if x<y then exit(x) else exit(y);
10 end;
11 begin
12     assign(input,'substring.in');
13     reset(input);
14     assign(output,'substring.out');
15     rewrite(output);
16     readln(n,m,k);
17     readln(a);
18     readln(b);
19     fillchar(ans,sizeof(ans),0);
20     for i:=0 to n do
21         ans[i,0,0]:=1;
22     for i:=1 to n do
23         for j:=1 to min(i,k) do
24             for p:=1 to min(i,m) do
25             begin
26                 ans[i,j,p]:=ans[i-1,j,p];
27                 q:=i;
28                 r:=p;
29                 while (q>=1)and(r>=1)and(a[q]=b[r]) do
30                 begin
31                     ans[i,j,p]:=(ans[i,j,p]+ans[q-1,j-1,r-1]) mod 1000000007;
32                     dec(q);
33                     dec(r);
34                 end;
35             end;
36     writeln(ans[n,k,m]);
37     close(input);
38     close(output);
39 end.
substring.pas

 T6 transport AAAAAATTTTTEETTTTEEET

  1 program _transport;
  2 var
  3     n,m:longint;
  4     a,b,t:array[1..600000] of longint;
  5     u,v:array[1..300000] of longint;
  6     w:array[1..300000] of longint;
  7     vis:array[1..300000] of boolean;
  8     father:array[1..300000] of longint;
  9     i,j,k,l,p:longint;
 10     ans:int64;
 11 function max(x,y:longint):longint;
 12 begin
 13     if x>y then exit(x) else exit(y);
 14 end;
 15 function min(x,y:longint):longint;
 16 begin
 17     if x<y then exit(x) else exit(y);
 18 end;
 19 procedure qsort(l,r:longint);
 20 var
 21     i,j,x,y:longint;
 22 begin
 23     i:=l;
 24     j:=r;
 25     x:=a[(l+r) div 2];
 26     repeat
 27         while a[i]<x do
 28             inc(i);
 29         while a[j]>x do
 30             dec(j);
 31         if i<=j then
 32         begin
 33             y:=a[i]; a[i]:=a[j]; a[j]:=y;
 34             y:=b[i]; b[i]:=b[j]; b[j]:=y;
 35             y:=t[i]; t[i]:=t[j]; t[j]:=y;
 36             inc(i); dec(j);
 37         end;
 38     until i>j;
 39     if l<j then
 40         qsort(l,j);
 41     if i<r then
 42         qsort(i,r);
 43 end;
 44 function checkline:boolean;
 45 var
 46     i:longint;
 47 begin
 48     if (a[1]<>1)or(b[1]<>2) then
 49         exit(false);
 50     if (a[2*n-2]<>n)or(b[2*n-2]<>n-1) then
 51         exit(false);
 52     for i:=2 to n-2 do
 53         if ((a[i*2-2]<>i)or(b[i*2-2]<>i+1))and((a[i*2-1]<>i)or(b[i*2-1]<>i+1)) then
 54             exit(false);
 55     exit(true);
 56 end;
 57 function find(data,l,r:longint):longint;
 58 var
 59     mid:longint;
 60 begin
 61     mid:=(l+r) div 2;
 62     if (a[mid]>=data)and(a[mid-1]>=data) then
 63         exit(find(data,l,mid-1));
 64     if (a[mid]<data) then
 65         exit(find(data,mid+1,r));
 66     if (a[mid]=data)and((mid=1)or(a[mid-1]<data)) then
 67         exit(mid);
 68 end;
 69 procedure maketree(x:longint);
 70 var
 71     i,j,k:longint;
 72 begin
 73     vis[x]:=true;
 74     j:=find(x,1,2*n-2);
 75     if x=n then
 76         k:=2*n-2
 77     else
 78         k:=find(x+1,1,2*n-2);
 79     for i:=j to k-1 do
 80     begin
 81         if not(vis[b[i]]) then
 82         begin
 83             father[b[i]]:=x;
 84             maketree(b[i]);
 85         end;
 86     end;
 87 end;
 88 function findfather(x,y:longint):longint;
 89 var
 90     i,lsx,lsy,sum:longint;
 91 begin
 92     fillchar(vis,sizeof(vis),0);
 93     lsx:=x;
 94     lsy:=y;
 95     while x<>0 do
 96     begin
 97         vis[x]:=true;
 98         x:=father[x];
 99     end;
100     while y<>0 do
101     begin
102         if vis[y] then
103             break;
104         y:=father[y];
105     end;
106     sum:=0;
107     while lsx<>y do
108     begin
109         inc(sum,w[lsx]);
110         lsx:=father[lsx];
111     end;
112     while lsy<>y do
113     begin
114         inc(sum,w[lsy]);
115         lsy:=father[lsy];
116     end;
117     exit(sum);
118 end;
119 begin
120     assign(input,'transport.in');
121     reset(input);
122     assign(output,'transport.out');
123     rewrite(output);
124     readln(n,m);
125     for i:=1 to n-1 do
126     begin
127         readln(a[i],b[i],t[i]);
128         t[i+n-1]:=t[i];
129         a[i+n-1]:=b[i];
130         b[i+n-1]:=a[i];
131     end;
132     for i:=1 to m do
133         readln(u[i],v[i]);
134     qsort(1,2*n-2);
135     if checkline then
136     begin
137         fillchar(w,sizeof(w),0);
138         w[1]:=t[1];
139         w[n-1]:=t[2*n-2];
140         for i:=2 to n-2 do
141         begin
142             if (a[i*2-2]=i)and(b[i*2-2]=i+1) then
143                 w[i]:=t[i*2-2]
144             else
145                 w[i]:=t[i*2-1];
146         end;
147         ans:=$3f3f3f3f;
148         for i:=1 to n-1 do
149         begin
150             l:=0;
151             for j:=1 to m do
152             begin
153                 p:=0;
154                 for k:=u[j] to v[j]-1 do
155                 begin
156                     if k<>i then
157                         p:=p+w[k];
158                 end;
159                 if l<p then
160                     l:=p;
161             end;
162             if ans>l then
163                 ans:=l;
164         end;
165         writeln(ans);
166     end
167     else
168     begin
169         fillchar(vis,sizeof(vis),0);
170         fillchar(father,sizeof(father),0);
171         maketree(1);
172         fillchar(w,sizeof(w),0);
173         for i:=1 to 2*n-2 do
174             if father[a[i]]=b[i] then
175                 w[a[i]]:=t[i];
176         ans:=$3f3f3f3f;
177         for k:=2 to n do
178         begin
179             j:=0;
180             l:=w[k];
181             w[k]:=0;
182             for i:=1 to m do
183             begin
184                 if findfather(u[i],v[i])>j then
185                     j:=findfather(u[i],v[i]);
186             end;
187             if ans>j then
188                 ans:=j;
189             w[k]:=l;
190         end;
191         writeln(ans);
192     end;
193     close(input);
194     close(output);
195 end.
transport.pas

其中大部分是不该失分的,正常水平应该在100+100+80(看常数,COGS上AC)+100+100+80=560......

posted @ 2015-11-23 19:59  常可  阅读(311)  评论(0编辑  收藏  举报