NOIP2004 Universal Group

Source: Internet
Author: User

T2:

Mr. Robinson has a pet monkey named Toto. This day, they are walking along the country road, suddenly found on the roadside signs with a small note: "Welcome to free tasting my peanut!" --Bear word ".
Mr. Robinson and Toto are very happy, because peanuts are their favorite. Behind the sign, there really is a peanut field on the roadside, and the peanut plants are neatly arranged in a rectangular grid (1). With a lot of experience, you can see how many peanuts are in each peanut plant. In order to train a lot of arithmetic, Mr. Robinson said: "You first find the most peanuts plant, to pick its peanuts, and then find the remaining plants in the most peanuts, to pick its peanuts; and so on, but you must be in my limited time to return to the roadside. ”


We assume that a lot can do one of the following four things in each unit time:
1) jump from the roadside to a peanut plant closest to the roadside (i.e. the first row);
2) jump from one plant to another plant adjacent to or around it;
3) Picking peanuts under a plant;
4) Jump back to the roadside from a peanut plant closest to the roadside (i.e. the first row).
Now given the size of a peanut field and the distribution of peanuts, in the limited time, how many can the maximum number of peanuts to be harvested? Note that it is possible that only some of the plants have peanuts under them, assuming that the number of peanuts under these plants varies.

For example, in the Peanut field shown in Figure 2, there are peanuts in the plants (2, 5), (3, 7), (4, 2), (5, 4), respectively, 13, 7, 15, 9. Along the route of the diagram, a maximum of 37 peanuts can be harvested in 21 unit time.

Input
The first line of the input file peanuts.in consists of three integers, M, N and K, separated by a space, which indicates that the peanut field size is M * N (1 <= m, n <= 20), and the limit time for a lot of peanuts is K (0 <= k <= 1000) unit time. The next M-line, each line includes n non-negative integers, and is separated by a space; the J-Integer pij (0 <= pij <= 500) of line I + 1 indicates the number of peanuts under the Peanut field plant (i, j), and 0 indicates that there are no peanuts under the plant.
Output
The output file Peanuts.out includes one row, which contains only an integer, that is, the maximum number of peanuts that can be harvested within a limited time.

Sample input
6 7 21
0 0 0 0 0 0 0
0 0 0 0 13 0 0
0 0 0 0 0 0 7
0 15 0 0 0 0 0
0 0 0 9 0 0 0
0 0 0 0 0 0 0


Sample output
37

Data range limits
Tips
"Sample Input 2"
6 7 20
0 0 0 0 0 0 0
0 0 0 0 13 0 0
0 0 0 0 0 0 7
0 15 0 0 0 0 0
0 0 0 9 0 0 0
0 0 0 0 0 0 0
"Sample Output 2"
28


This problem is pure simulation ah, careful point can be AC.

Code:

var c:array[0..400,1..3] of Longint;        A:ARRAY[1..20,1..20] of Longint; M,n,k,i,j,len,ans,way,x,y:longint;procedure sort (l,r:longint); var i,j,mid,p:longint;begin i:=l;        J:=r;        mid:=c[(l+r) div 2,1];                While I<j does begin while C[i,1]>mid do Inc (i);                While C[j,1]<mid do Dec (j); If I<=j then begin c[0]:=c[i]; C[I]:=C[J];                        C[J]:=C[0]; Inc (I);                Dec (j);        End        End        If L<j then sort (l,j);        If I<r then sort (i,r); End;begin readln (M,n,k);                        For I:=1 to M does for J:=1 to n do begin read (A[i,j]);                                If A[i,j]>0 then BEGIN Inc (LEN);                                C[LEN,1]:=A[I,J];                                C[len,2]:=i; C[len, 3]:=j;                End        End        Sort (1,len);                If C[1,2]*2+1>k then begin Writeln (0);        Halt        End ans:=c[1,1]; x:=c[1,2]; y:=c[1,3];        way:=x+1;                        For i:=2 to Len does if Way+abs (c[i,2]-x) +abs (c[i,3]-y) +1+c[i,2]<=k then BEGIN                        Inc (ans,c[i,1]);                        Way:=way+abs (c[i,2]-x) +abs (c[i,3]-y) +1;                        x:=c[i,2];                y:=c[i,3];        End else break; Writeln (ans); end.


T3:

We can divide the string consisting of "0" and "1" into three categories: all "0" strings are called B-strings, all "1" strings are called I-strings, and strings containing "0" and "1" are called F-strings.
The FBI tree is a binary tree [1], and its node types also include F-nodes, B-nodes, and I-nodes three species. By a 2N length of "01" string s can be constructed out of an FBI tree T, recursive construction method is as follows:

The root node of T is R, and its type is the same as the type of string s;
If the length of the string s is greater than 1, the string s is separated from the middle, divided into equal-length left and right substrings S1 and S2; the left subtree of R is constructed by the left substring S1 T1, and the right sub-tree of R is S2 by the right child string.
Now given a "01" string of length 2N, construct an FBI tree with the above construction method and output its sequential traversal [2] sequence.
[1] binary tree: Two fork tree is a finite set of nodes, this set is either an empty, or by a root node and two disjoint two fork tree composition. These two disjoint two-fork trees are called the Saozi right subtree of this root node, respectively.
[2] post-order traversal: post-sequence traversal is a method of depth-first traversal of the binary tree, its recursive definition is: sequential traversal of the left sub-tree, and then the subsequent traversal of the right sub-tree, the last access to the root.


Input
The first line of the input file fbi.in is an integer N (0 <= N <= 10), and the second row is a "01" string with a length of 2N.
Output
The output file Fbi.out includes a line that contains only one string, which is the post-order traversal sequence of the FBI tree.

Sample input
3
10001011
Sample output
Ibfbbbfibfiiiff


"Data Size"
For 40% of data, N <= 2;
For all data, N <= 10.


FBI tree this title is DG a string, and then according to the title of the output can be, very simple, code:

var        n:longint;        S:ansistring;procedure DFS (s:ansistring); var        i:longint;        Bz1,bz2:boolean;begin        If Length (s) >1 then        begin                Dfs (copy (s,1,length (s) Div 2));                DFS (copy (s,length (s) div 2+1,length (s) Div 2));        End;        Bz1:=false;        Bz2:=false;        For i:=1 to Length (s) does                if s[i]= ' 0 ' then bz1:=true else bz2:=true;        If Bz1 and bz2 then write (' F ') else        if the bz1 then     write (' I ') Else                            write (' B '); end;begin        readln (n); C18/>READLN (s);        DFS (s); end.



T4:

The man finally ascended the land of Mars and saw the mysterious Martians. Neither humans nor Martians can understand each other's language, but our scientists have invented a method of digital communication. This is the way of communication, first of all, the Martians told a very large number of human scientists, scientists cracked the meaning of the figure, and then put a very small number on this large number, the results told the Martians, as the human answer.
Martians use a very simple way to represent numbers--breaking fingers. The Martians had only one hand, but there were thousands of fingers on the hand, and the fingers were lined up in a column numbered three-in-one. Any of the two fingers of a Martian can exchange positions at will, and they are counted by this method.
A Martian uses a human hand to demonstrate how to count with a finger. If the five fingers-thumb, forefinger, middle finger, ring finger and pinky are numbered 1,2,3,4 and 5 respectively, when they are arranged in the normal order, forming 5 digits 12345, when you exchange the position of the ring finger and the little finger, will form 5 digits 12354, when you put five fingers in the order completely reversed, Will form 54321, in all the 120 5 digits that can be formed, 12345 the smallest, which represents 1;12354 second small, which represents 2;54321 Max, which represents 120. The following table shows the 6 3-digit numbers that can be formed when there are only 3 fingers and the number they represent:


Three-in-number

123

132

213

231

312

321

The number represented

1

2

3

4

5

6


Now you are fortunate to be the first Earth man to communicate with the Martians. A Martian will let you look at his finger, and the scientist will tell you to add a very small number of them. Your task is to add the numbers that the Martians use their fingers to the numbers scientists tell you, and to change the order of the Martians ' fingers according to the added results. The input data guarantees that this result will not exceed the range that the Martian finger can represent.


Input
The input file martian.in includes three lines, the first line has a positive integer N, which represents the number of Martian fingers (1 <= N <= 10000). The second line is a positive integer M, which represents the small integer to be added (1 <= M <= 100). The next line is an arrangement of 1 to N of n integers, separated by spaces, to indicate the order in which the Martian fingers are arranged.
Output
The output file martian.out only one row, which contains n integers, indicating the order in which the changed Martian fingers are arranged. Each of the two adjacent numbers is separated by a space and cannot have extra spaces.

Sample input
5
3
1 2 3) 4 5
Sample output
1 2 4) 5 3

Data range limits
Tips
"Data Size"
For 30% of data,n<=15;
For 60% of data,n<=50;
For all data,n<=10000;


Three-in-number

123

132

213

231

312

321

The number represented

1

2

3

4

5

6


This problem is purely analog, do not use the whole arrangement of the method of construction, throughout the data you will be the pit miserable, only with the method of the simulation of the whole arrangement. A very simple simulation--
var c:array[0..10000] of Longint;        BZ:ARRAY[1..10001] of Boolean;        A:ARRAY[1..10000] of Longint;        N,m,i,j,k,len,w,q:longint;begin READLN (n);        READLN (m);        For I:=1 to n do read (A[i]);                        For I:=1 to M does for j:=n-1 Downto 1 do Beginfillchar (bz,sizeof (BZ), false);                        For k:=j+1 to n do bz[a[k]]:=true;                        For w:=a[j]+1 to N does if bz[w] then break;                                If BZ[W] then begin Q:=A[J];                                Bz[q]:=true;                                A[j]:=w;                                Bz[a[j]]:=false;                                c[0]:=0;                                len:=0; For K:=1 to N does if bz[k] then isGin Inc (C[0]);                                        C[c[0]]:=k;                                End                                        For k:=j+1 to N Do BEGIN Inc (LEN);                                A[k]:=c[len];                                                                End                        Break                                        End        End        For I:=1 to N-1 do write (A[i], "); Writeln (A[n]); end.



of course, there is another way to do this is to convert the simulation to a string on the original string, which is easy to handle. the code is short and easy to implement:
var a:array[1..10000] of Longint;        BZ:ARRAY[1..10000] of Boolean;        N,m,i,tot:longint; Flag:boolean;procedure DFS (K:longint), var i:longint;begin if K>n then BEGIN Inc (TOT                );                Flag:=false;                         If Tot=m then the begin for I:=1-n do write (A[i], ");                Halt                End        Exit        End                        For I:=1 to N does if (Bz[i]) and (a[k]<i) then begin Bz[i]:=false;                        A[k]:=i;                        DFS (K+1);                        a[k]:=0;                Bz[i]:=true;        End;end;begin readln (n,m);        For I:=1 to n do read (A[i]);                For i:=n Downto 1 does begin bz[a[i]]:=true;                DFS (i);        a[i]:=0; End;end.


NOIP2004 Universal Group

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.