Noip2008 Problem Solving report

Source: Internet
Author: User

The questions in 2008 were relatively simple and not very troublesome. You can write them right.

 

Question 1:

Give a lowercase word, and find the occurrence times of the least Letter and the number of occurrences of the most letter.

 

Solution Process: directly simulate hash.

 

Question 2:

How many pieces of equation, like "A + B = C", can you spell out n matchsticks? A, B, and C in the equation are integers spelled out by matchsticks (if the number is not zero, the maximum digit cannot be 0 ). Match 0-9 with a matchstick:

Note:

1. Two matchsticks are required for the plus sign and equal sign respectively.

2. If a = B, a + B = C and B + A = C are considered different equations (a, B, c> = 0)

3. All n matchsticks must be used. N <= 24

 

Problem solving process:

1. When I first saw a + B = C, I thought of enumeration directly, but there may also be 1111 + 0 = 1111 .. Therefore, it is difficult to determine the enumerated range.

2. So I decided to use the DFS two Addons to check whether the remaining matches can be combined. For any number, you can add 0 to the end of it ~ The price of the number 9 is the number of matches required, so that they are not repeated. There is a small optimization, but I am too lazy to write, that is, make the first number not greater than the second number. When the result is found, if the first number is equal to the second number, ANS + 1, otherwise, ANS + 2. This can reduce the search volume by half, but it does not matter if the data is so small that it is not written.

 

Question 3:

The question is: the deformation of the number of squares. Two people walk together, and the other person can no longer walk.

Problem solving process:

The question is almost the same as the question of getting the number on the square in 2000. However, in year 00, We could go where we walked, but we only got a score of 0. so we only need to change it in the dynamic planning equation of the question in year 00, f [k] [I] [J] =-INF. f [k] [I] [J] indicates that step K is taken. The first person goes down to step I, and the second person goes down to the maximum score of step J.

Code for dynamic planning:

 1     for (int k=1;k<=n+m-2;k++) 2         for (int i=0;i<n;i++) 3             for (int j=0;j<n;j++) 4             { 5                 if (i==j&&k!=n+m-2) 6                 { 7                     g[k][i][j]=-210000000; 8                     continue; 9                 }10                 if (i!=0 && j!=0)11                     g[k][i][j]=ma(g[k][i][j],g[k-1][i-1][j-1]);12                 if (i!=0 && j!=k)13                     g[k][i][j]=ma(g[k][i][j],g[k-1][i-1][j]);14                 if (i!=k && j!=k)15                     g[k][i][j]=ma(g[k][i][j],g[k-1][i][j]);16                 if (i!=k && j!=0)17                     g[k][i][j]=ma(g[k][i][j],g[k-1][i][j-1]);18                 g[k][i][j]+=a[i+1][1+k-i]+a[j+1][1+k-j];19             }


Question 4:

Tom is studying an interesting sorting problem recently ., With two stacks S1 and S2, Tom wants to sort input sequences in ascending order using the following four operations.

Operation

If the input sequence is not empty, press the first element into Stack S1.

Operation B

If Stack S1 is not empty, the top element of stack S1 is popped up to the output sequence.

Operation C

If the input sequence is not empty, press the first element into Stack S2.

Operation d

If Stack S2 is not empty, the top element of stack S2 is displayed to the output sequence.

If ~ The arrangement of n p allows the output sequence to be 1, 2,… through a series of operations ,..., (N-1), N, Tom said P is a "double stack sorting ". For example, (,) is a "dual-stack sorting sequence", and (,) is not. Describes an operation sequence for sorting (,): <A, C, C, B, A, D, D, B>

Of course, there may be several such operation sequences. For the above example (,), <A, C, C, B, A, D, D, b> is another feasible operation sequence. Tom wants to know what the operation sequence with the smallest Lexicographic Order is.

 

Problem solving process:

1. I manually simulated it at the beginning, and I felt greedy. If the current element is larger than the top element of the stack, it cannot be placed on the top of the stack. (Similar to the tower of Hanoi .) The greedy policy is as follows: if you can store data on stack a, try to store data on stack a. Otherwise, you can store data on Stack B. If neither of them can be placed, no solution is output. If the current element is the next element to be added to the output queue, stack a is followed, that is, 'A' + 'B' is operated ', then, use a while to get all the results .. Soon, I wrote about B in stack writing, but I can submit only five points .. The exam is too late to write like this ..

2. Positive Solution: Refer to blog http://blog.csdn.net/kqzxcmh/article/details/9566813 to write very clearly, the Code is also simple and easy to understand, short and concise ..

First, according to the stack nature, the s [I] And s [J] elements cannot enter the same stack. <=> K exists, meeting the requirements of I <j <K, so that s [k] <s [I] <s [J]. it will not prove, but it is absolutely correct .. To put it simply, for an element in the inbound stack sequence, the elements before it and larger than it must increase progressively (from right to left, from left to right .) This is easy to understand, but K is still used for programming, satisfying I <j <K, so that s [k] <s [I] <s [J.

Therefore, you only need to find all the number pairs (I, j) that cannot appear in the same stack sequence at the same time, so that the previous elements can be pushed to stack a as much as possible. This is actually a process of determining a bipartite graph. If it is not a bipartite graph, it will inevitably have no solution. If yes, simulate the stack.

My code:

 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4  5 char ans[2014]; 6 int n,a[1010],stack1[1010],stack2[1010],top1,top2,cur; 7 int p[1010],q[1010]; 8 int g[1010],color[1010],edge[1010][1010],flag=1; 9 10 void pop()11 {12     while (stack1[top1]==cur||stack2[top2]==cur)13     {14         if (stack1[top1]==cur)15         {16             cur++;17             top1--;18             printf("b ");19         }20         else21         {22             cur++;23             top2--;24             printf("d ");25         }26     }27 }28 29 void dfs(int x,int c)30 {31     if (!flag)32         return;33     color[x]=c;34     for (int i=1;i<=n;i++)35         if (edge[x][i])36         {37             if (color[i]==c)38             {39                 flag=0;40                 return;41             }42             if (!color[i])43                 dfs(i,3-c);44         }45 }46 47 void solve()48 {49      g[n+1]=210000;50      for (int i=n;i>=1;i--)51          g[i]=a[i]<g[i+1]? a[i]:g[i+1];52      for (int i=1;i<n;i++)53           for (int j=i+1;j<=n;j++)54               if (a[i]<a[j]&&g[j+1]<a[i])55                   edge[i][j]=edge[j][i]=1;56      for (int i=1;i<=n;i++)57          if (!color[i])58              dfs(i,1);59      if (!flag)60      {61          cout<<0<<endl;62          return;63      }64      cur=1;65      stack1[0]=stack2[0]=210000;66      for (int i=1;i<=n;i++)67      {68           if (color[i]==1)69          {70             printf("a ");71             stack1[++top1]=a[i];72          }73          else74          {75              printf("c ");76              stack2[++top2]=a[i];77          }78          pop();79      }80      pop();81 }82 83 int main()84 {85     //freopen("in.txt","r",stdin);86     //freopen("out.txt","w",stdout);87     cin >> n;88     for (int i=1;i<=n;i++)89         scanf("%d",&a[i]);90     solve();91     return 0;92 } 

 

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.