"Algorithmic Learning note" 89. Sequential dynamic programming SJTU OJ 4020 Series game

Source: Internet
Author: User

http://acm.sjtu.edu.cn/OnlineJudge/problem/4020

One of the first to get started is a sort of +dfs .... Although the correctness is correct, but the timeout is only 60 points. Mostly because we don't know how to reduce the amount of search.

The idea is to delete some of the points that must not be in, and then conditionally Judge Dfs to search for the longest path

#include <iostream>#include<vector>#include<algorithm>#include<cstring>#include<stack>using namespacestd;intN;intdata[ ++Ten];BOOLvis[ ++Ten]={false};structpoint{intOripos; intnum; intTodo; intvid; intDone ; intLen; Point (intXinty) {Oripos=x; Num=y; Todo= Oripos-num; Vid= -1; Done=0; Len=0; }};vector<Point>v;BOOLCmp_point (Constpoint& A,Constpoint&b) {    returnA.num <B.num;} voidinit () {v.clear ();  for(inti =1; I <= N; ++i) {cin>>Data[i]; if(data[i]<=i)            {Point T (I,data[i]);        V.push_back (t);    }} sort (V.begin (), V.end (), cmp_point); memset (Vis,false,sizeof(Vis));} //must DFSintbuild () { for(inti =0; I < v.size (); ++i) {//cout<<v[i].oripos<< "," <<v[i].num<< "," <<v[i].todo<<endl;V[i].vid =i; }        intAns =0; Stack<Point>s; Point Start (0,0);    S.push (start);  while(!S.empty ()) {Point cur=S.top ();        S.pop (); if(Cur.vid >=0) Vis[cur.vid]=true;  for(intj = cur.vid+1; J < V.size (); ++j) {if(cur.vid==-1or (V[j].oripos > V[cur.vid].oripos and V[j].todo-cur.done >=0and v[j].num!=v[cur.vid].num)) {Vis[j]=true; Point Next (0,0); Next.oripos= V[j].oripos; Next.num =V[j].num; Next.len= cur.len+1; Next.done=V[j].todo; Next.vid=J; Ans=Max (Next.len,ans);            S.push (next); }        }    }    returnans;} intMainintargcChar Const*argv[]) {     while(cin>>N)        {init (); cout<<build () <<Endl; }    return 0;}
DFS

The right way to do it is to DP.

Here to see the hint in the title, he stressed that from the N to erase a few.

This some hint of the last DP with two-dimensional +for traversal

That is, our dp[i][j] to be expressed as the number of the first I, erase a J of the remaining number of matching conditions

Finally we output Max (dp[n][k]) k = 1....N; Can

The transfer equation is as follows

dp[0][0] =0;  for(inti =1; I <= N; ++i) {dp[i][0] = (data[i]==i)? dp[i-1][0]+1: dp[i-1][0];  for(intj =1; J < I; ++j) {        if(data[i]== (I-1)-j) +1) {Dp[i][j]= dp[i-1][J] +1; }Else{Dp[i][j]= Max (dp[i-1][j-1],dp[i-1][j]); }} Dp[i][i]=0;}

Explain

We discuss the case of dp[i][j] to be divided into J.

1. j=0 so at this time dp[i][0] There are two cases,

If data[i] = I means that data[i] itself is in the original position then the number of qualifying numbers is the previous I-1 number of qualifying numbers +1, dp[i][0] = dp[i-1][0]+1

Otherwise, it's the same as before.

2.j=i dp[i][i]=0 This is good to understand before I a number of erase I finally meet the conditions of the affirmation is 0 .... Because there's no count.

3.J = I-1,...,

There are two cases, one is that if our data[i] happens to be the first i-1 number to erase the position of J, it accumulates 1.

For example i = 5, J =2 If the first 4 numbers, we wiped 2 and then there are two legal, we add a number in the back of his position is 3 if we attach this number is 3, then the first 5 count, there are 3 legal

So at this time dp[i][j] = Dp[i-1][j] +1

If this condition is not met

So consider the first I number to remove a J There are two ways to remove the first kind of all the number of erase is i-1 inside the so is dp[i-1][j]

The second is the j-1 is the first i-1 inside the remaining one is the first element so it is dp[i-1][j-1]

Choose the largest of the two.

The code is as follows (many of the comments are not clear)

#include <iostream>#include<vector>#include<algorithm>#include<cstring>#include<stack>#include<cstdio>using namespacestd;Const intMAXN = ++Ten;intN;intDATA[MAXN];intdp[maxn][maxn]={0};//Dp[i][j] Represents the number of the first I, to remove a J when the remaining number of matching conditions of several numbersvoidinit () { for(inti =1; I <= N; ++i) {scanf ("%d",&Data[i]); }    //the initialization of the DP    }intbuild () {//Dp[i][j] Represents the number of the first I, to remove a J when the remaining number of matching conditions of several numbersdp[0][0] =0;  for(inti =1; I <= N; ++i) {//divided into three stages of processing//First paragraph j=0dp[i][0] = (data[i]==i)? dp[i-1][0]+1: dp[i-1][0];//If I is the number of I, update +1.//second paragraph j=1,2,3...i-1         for(intj =1; J < I; ++j) {            //I subtract J, so there's i-j left.//if (data[i]== (i-1)-j + 1)//I've subtracted the J number from the i-1. Our position is close to the next +1//Dp[i][j] = dp[i-1][j] + 1; //Else//You can't put it. Select a maximum from the previous two states//     //one may be the former i-1 to wipe away j-1 a hold I//     //the other may be the former i-1 to erase J and then erase I//Dp[i][j] = max (dp[i-1][j-1],dp[i-1][j]);            if(data[i]== (I-1)-j) +1) {Dp[i][j]= dp[i-1][J] +1; }Else{Dp[i][j]= Max (dp[i-1][j-1],dp[i-1][j]); }         }        //Third paragraph j=iDp[i][i] =0;//the first I wiped out I a sure result was 0    }    intAns =0; //Note Be sure to start from 0 or else there are loopholes such as n=4:1 2 3 4 This situation     for(inti =0; I <= N; ++i) {ans=Max (ans,dp[n][i]); }    returnans;}intMainintargcChar Const*argv[]) {     while(cin>>N)        {init (); cout<<build () <<Endl; }    return 0;}/*void Init1 () {//v.clear ();        for (int i = 1; I <= n; ++i) {//cin>>data[i];        scanf ("%d", &data[i]);        if (data[i]<=i) {//Point T (I,data[i]);        V.push_back (t);    }}//sort (V.begin (), V.end (), cmp_point); memset (vis,false,sizeof (Vis));} int Build2 () {for (int i = 0; i < v.size (); ++i) {//cout<<v[i].oripos<< "," <<v[i].num&lt        ;< "," <<v[i].todo<<endl;    V[i].vid = i;    } int ans = 0;    Stack<point> s;    Point Start (0,0);    S.push (start);        while (!s.empty ()) {Point cur = s.top ();        S.pop ();        if (cur.vid >= 0) Vis[cur.vid] = true;             for (int j = cur.vid+1; J < V.size (), ++j) {if (V.size () -1-cur.vid + Cur.len < ans) break; if (Cur.vid==-1 or (V[j].oripos > V[cur.vid].oripos and v[j].todo-cur.done >= 0 and V[j].num!=v[cur.vid]. num)) {Vis[j] = True;                Point next (0,0); Next.oripos = V[j].oripos;                Next.num = V[j].num;                Next.len = cur.len+1;                Next.done = V[j].todo;                Next.vid = j;                ans = max (Next.len,ans);            S.push (next); }}} return ans;}*///1 2 3 4 5 6 7 8 9 -each of the Ten//4 6 2, 9 5, 2 7, 1 3, 9//5 2 7 1 3 8//5 7 8 dfs ....//4 6 2, 9 5, 2 7, 1 3, 9/*struct point{int oripos;    int num;    int todo;    int vid;    int done;    int Len;        Point (int x,int y) {oripos = x;        num = y;        Todo = Oripos-num;        vid =-1;        Done = 0;    len = 0; }}; for (int i = 0; i < v.size (); ++i) {int len = 1;//head int done = v[i].todo;//The left has been reduced by how many numbers int cur = i;//The current pointer for (Int j = i+1; J < V.size (); ++J) {if (V[j].oripos > V[cur].oripos and v[j].todo-done >= 0 and v[j].num!=v[cur].num) {cur = j; done = V[j].todo; l en++; }} ans = max (Ans,len); }  */
View Code

"Algorithmic Learning note" 89. Sequential dynamic programming SJTU OJ 4020 Series game

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.