[Puzzle + summary] Dynamic planning of the cluster II

Source: Internet
Author: User
Tags bitwise operators

1. Preface

A total of 14 questions, from the hand of Jianggo (this is not a good show), do let people squid chatter. Although most of the noip is difficult, there are a few simple questions, but still do very hard, there are few questions almost no idea, the following side of the road to look at the analysis.

2. The longest ascending subsequence sequence of lis

The only naked problem, but O (n^2), temporarily looked at O (n log n) of the dichotomy and line-tree approach. First of all, the simple dichotomy, whose essence is to optimize on O (n^2), needs to prove a conclusion. The current processing sequence K-bit, exists:

(1) a[i]<a[j]<a[k];

(2) i<j<k;

(3) F[i]=f[j].

At this time we choose A[i] good or a[j] good? Apparently a[i] good. Why? If A[i]<a[x]<a[j],i<x<j is present, select A[i] to get a longer subsequence--a[i],a[x],a[k] ... Thus, for f[t]=k, we only need to save A[t] the smallest value of the T, you can prove that the F array will be monotonically increment, so we do not need to go forward every bit to find, and can do binary search, so the time complexity of O (n log n).

Code:

------------------------------------------------------------------------------------------------------

#include <cstdio>

int min (int a,int b) {return (a<b) a:b;}

int V[200010],n,x,ans;

int main ()
{
freopen ("lis.in", "R", stdin);
freopen ("Lis.out", "w", stdout);
scanf ("%d", &n);
for (int i=1;i<=n;i++)
{
scanf ("%d", &x);
int l=1,r=ans,mid= (l+r) >>1,loc=0;
While (l<=r)
{
if (v[mid]<=x) loc=mid,l=mid+1;
else r=mid-1;
mid= (l+r) >>1;
}
if (Loc==ans) v[++ans]=x;
Else V[loc+1]=min (v[loc+1],x);
}
printf ("%d\n", ans);

}

------------------------------------------------------------------------------------------------------

3. Chess Black and white board

According to the analysis of the problem, we need to maintain the existence of a pawn in the column, whether there is a pawn in the row, and because of n<=15, we can consider the binary state compression to transfer, up to 2^ (n+1) state. Set F[I][J] As the current line I, the number of scenarios when the state is J (compressed binary state). Today I just learned to use the &, ^, |, and other bitwise operators, the last time I wrote a pressure or handwritten ... Equation: f[i][j]=sum{f[i-1][j^ (1<< (k-1))} (k∈[1,n],j& (1<< (k-1)),!a[i][k])

Code:

------------------------------------------------------------------------------------------------------

#include <cstdio>
#define MOD 1e9+7

int N,a[20][20],f[20][100000],ans;
Char ch;

int main ()
{
freopen ("chess.in", "R", stdin);
freopen ("Chess.out", "w", stdout);
scanf ("%d", &n);
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
{
Do Ch=getchar (), while ((ch!= ' 0 ') && (ch!= ' 1 '));
a[i][j]=ch-' 0 ';
}
f[0][0]=1;
for (int i=1;i<=n;i++)
for (int j=0;j<1<<n;j++)
{
F[i][j]=f[i-1][j];
for (int k=1;k<=n;k++)
if ((j& (1<< (k-1)) && (!a[i][k]))//If status J and
{
f[i][j]+=f[i-1][j^ (1<< (k-1))];
if (f[i][j]>=mod) f[i][j]-=mod;
}
}
for (int i=0;i<1<<n;i++) {ans+=f[n][i]; if (ans>=mod) ans-=mod;}
printf ("%d", ans);
return 0;
}

------------------------------------------------------------------------------------------------------

4, bound multi-knapsack problem

I believe this problem should be in the examination room few people can make ah ... It's hard to prove it. It was really surprising. The problem itself does not seem to be very difficult, but we notice that the data range is <=10^9, which is an unacceptable length for even O (n). At this point, consider some modifications to the path to meet our scope requirements. First find out 1-10 of any two number of least common multiple, and then take the maximum Max, you can prove that when the distance between the two stones is greater than Max, then each point greater than its part can be a combination of these two numbers to jump, so when the distance between two stones is greater than this least common multiple, Then narrow their distances to this least common multiple. I still do not know whether this statement is correct, because there are some said on the internet can be >=72 compression, and take the largest least common multiple can only be >=100 compression. Correctness remains to be verified. Use f[i] to indicate the minimum number of stones to be trampled when jumping to I. Equation:f[i]=min{f[i-k]+x} (k∈[s,t],x={0,1} to indicate if I have a pebble)

5. NOIP2006 Energy Necklace

Stone Merge reinforced version, because it is a ring, so we have to consider cutting it in advance. There is an O (n^4) approach--enumerating the cutting points, enumerating the midpoint on the basis of the left and right endpoints of the enumeration. Because the range of n<=100,10^8 is barely able to. For the enumeration of cutting points, can you consider optimization? We do not need to actually cut, set the array when the data can be copied once, and then go directly into the merge process. Use f[i][j] to indicate the maximum score from the number of numbers to the first J, then Ans=max{f[k][n+k+1]} (K∈[1,n-1]), f[k][n+k+1] means that the cutting point is between k-1 and K. Equation:f[i][j]=max{f[i][k]+f[k+1][j]+a[i]*a[k+1]*a[j+1]} (K∈[i,j-1])

6, NOIP2006 jinming budget plan

Knapsack problem-enhanced version, I did not think this practice, too weak ... Similarly, f[i][j] means that the first I items with a value of not more than J to get the maximum of the importance of the price, due to the presence of attachments, the purchase of attachments is the premise of the main part to buy, in the dynamic planning we can not be similar to the conditions in the search, this consideration will compress this requirement--when enumerating items, If the item is an attachment, skip it directly, and if the item is a main piece, make three transfers-individual buyer pieces, buyer's pieces and an accessory, buyer's pieces and two accessories. Thus, the equation of existence:F[i][j]=f[i-1][j-w[i]]+p[i]*v[i].

7, NOIP2007 matrix fetch number

n Rows m columns, because there is no connection between rows and rows, we plan dynamically for each row and then calculate the sum. F[I][J] represents the current line remaining number I to the number of J is not taken when the maximum score, there are taken to the left and take the right two kinds of cases, so f[i][j]=max{f[i][j-1]+a[j]*2^ (m-j+i), f[i+1][j]+a[i]*2^ (M-I+J)}. Due to the large data, high-precision addition is required.

8, NOIP2008 Pass the note

To be added.

9. NOIP2010 Turtle Chess

To be added.

10. NOIP2013 Gardener

The positive solution of this problem seems to be greedy? Find the inflection point on the line ... But dynamic planning is also possible. First, it is easy to think of an O (n^2) algorithm. Because the topic requires the sequence to maintain the wave shape, there are two cases, a high or a low (odd-even discussion), F[i][0] to the first flower as the end, and it is takahana when the maximum number of remaining flowers; F[i][1] represents the first flower as the end, and it is a dwarf flower of the maximum number of remaining flowers. Therefore, the equation can be obtained by alternating transfer according to the wave morphology:f[i][0]=max{f[j][1]}+1 (J∈[1,i-1],h[i]>h[j]); F[i][1]=max{f[j][0]}+1 (J∈[1,i-1],h[i] <H[J]).

But N<=100000,o (n^2) is obviously not going to make it. Can we omit one dimension? After looking at the puzzle, we have no need to use all the previous state in the transfer, just consider the relationship between the flower and the current flower. Why? According to test instructions, in judging the first flower, in [1,i-1] in accordance with the transfer condition the maximum value must be at the end, in line with local monotonicity. So we only need to pay attention to the state of the i-1 flower. F[i][0] and f[i][1], as above, according to H[i]>h[i-1],hh[i]F[i][0]=max (f [I-1] [1]+1,f[i-1][0]), f[i][1]=f[i-1][1] (h[i]>h[i-1]); F[i][0]=f[i-1][0],f[i][[1]=f[i-1][1] (h[i]=h[i-1]); f[i][0]=f [I-1] [0],f[i][1]=max (F[i-1][0]+1,f[i-1][1]) (H[i]

11, NOIP2014 Flying Birds

At that time there was nothing to directly hit a 50-point violence, in fact, it seems relatively easy to see this is a dynamic planning. The same bare DP has only 70 points and requires a monotonic queue optimization. To be added.

12. Summary

Now make a summary of the topic of dynamic planning. The difficulty of Noip is probably the above, I also in the brain to fill the big hole of the motion regulation, dynamic planning many topics to do problems in the way there is common: first we have to modify the operation to design the state of the problem, the state must be no Overlap exists individually (that is, for each element in the dynamic planning array corresponds to only one state), and we are able to get the answer through a linear shift.

Some states are obvious, easy to design, such as "chorus Formation", "Turtle Chess", "energy necklace" feeling to be able to see the approximate model, but some of the problems are vague, be sure to find the most appropriate state. For example, "Matrix Fetch", originally I set is f[i][j] for the current take away I, where from the left to take J when the maximum score. This is in line with the independence of the state, but it is inconvenient to transfer (and possibly I did not think at that time).

But after everything is done, be sure to verify that the complexity is correct. While most of the topics are part of the story, if dynamic planning can do AC, of course, it is necessary to consider the optimization, such as "gardener". Similar to the longest ascending subsequence there are two practices of O (n^2) and O (n log n). optimization, can be directly in the simple way, the same slope optimization, monotonous queue is also necessary.

[Puzzle + summary] Dynamic planning of the cluster II

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.