Missile: Dual-state DP, missile state dp

Source: Internet
Author: User

Missile: Dual-state DP, missile state dp
Question

Description

Long, long ago, country A specified Ted a missile system to destroy the missiles from their enemy. that system can launch only one missile to destroy multiple missiles if the heights of all the missiles form a non-decrease sequence.

But recently, the scientists found that the system is not strong enough. so they invent another missile system. the new system can launch one single missile to destroy more enemy missiles. basically, the system can destroy the missile from near to far. when the system is begun, it chooses one enemy missile to destroy, and then destroys a missile whose height is lower and farther than t He first missile. The third missile to destroy is higher and farther than the second missile... The odd missile to destroy is higher and farther than the previous one, and the even missile to destroy is lower and farther than the previous one.

Now, given you a list of the height of missiles from near to far, please find the most missiles that can be destroyed by one missile launched by the new system.

Input

The input contains multiple test cases.

In each test case, first line is an integer n (0 <n <= 1000), which is the number of missiles to destroy. then follows one line which contains n integers (<= 109), the height of the missiles followed by distance.

The input is terminated by n = 0.

Output

For each case, print the most missiles that can be destroyed in one line.

Sample Input

4
5 3 2 4
3
1 1 1
0

Sample output

3
1

A missile can destroy enemy missiles. However, there is a strange setting:
When an enemy missile comes from far and near, it can choose one of them to start to be destroyed, and then destroy a missile farther and at a lower height than the previous one, then they will destroy missiles that are farther but higher in height.
Calculate the maximum number of missiles.
The first thought of this question is the monotonically increasing sub-sequence deformation. That is, the longest concave-convex subsequence ..
It is obviously a dp problem.
The key to solving the dp problem is to sort out the state transition equation. For monotonically increasing subsequences, the state transition equation is: dp [I] = max (dp [k] + 1) (k <I & v [k] <v [I]);
That is, the longest monotonic subsequence ending with the current vertex is prior to this vertex; the value is smaller than this vertex; and the local optimum + 1 among them.
Because the problem is not monotonic increasing, we need to deform the dp equation.
It is assumed that the rule of missiles is high/low switching. Therefore, each missile may be shot down as a lower or as a high missile. Being shot down as a different identity may have different optimal solutions, and will also affect the subsequent missiles.
Therefore, we need to save two States. The simple convention is: a [I]: Missile I is the optimal solution when higher is shot down. B [I]: the missile is the optimal solution when lower is shot down.
Relatively, the new dp equation is: a [I] = max {B [k] + 1} (k <I & v [k] <v [I]) B [I] = max {a [k] + 1} (k <I & v [k]> v [I])
Initialize a [0] = 1 // The first hit I = 0 to get the 1 solution B [0] = 0 // because the first hit in the question is high, therefore, a missile at the position 0 cannot be shot down as a lower. As the lower, the optimal solution is 0.

Complete code: the code written earlier is not standardized. Let's take a look.
#include<iostream>using namespace std;int a[1001],b[1001];int str[1001];int main(){    //freopen("aa.txt","r",stdin);    int n,i,j,max2;    while(scanf("%d",&n)!=EOF)    {        if(!n)            break;        for(i=0;i<n;i++)            scanf("%d",&str[i]);        for(i=0;i<n;i++)        {            a[i]=1;  //as higher            b[i]=0;  //as lower        }        for(i=1;i<n;i++)        {            int max=1,max1=0;            for(j=0;j<i;j++)            {                if(str[i]>str[j])                {                    a[i]=b[j]+1;                    if(a[i]>max)                        max=a[i];                }                if(str[i]<str[j])                {                    b[i]=a[j]+1;                    if(b[i]>max1)                        max1=b[i];                }            }            b[i]=max1;            a[i]=max;        }        max2=a[0];        for(i=0;i<n;i++)        {            if(a[i]>max2)                max2=a[i];            if(b[i]>max2)                max2=b[i];        }        cout<<max2<<endl;    }}


Verify why the program ensures that the optimal solution is "first hit higher? It can be proved that the default B [I] is 0 because of all positions I. If the optimal solution appears at the lower position, that is, B [k] is the maximum value of all a [] B, it indicates that a [j]> B [k] must exist before k. that is to say, there is a higher first hit.
Simple test.





Which country is DP590 dual-phase steel, DP780 dual-phase steel, and DP980 dual-phase steel? Online rush

For enterprise standards of Baoshan Iron and Steel Co., Ltd. in our country, see: Q/BQB 418-2009
DP is dual phase steels dual-phase Steel and the number is tensile strength value.
 
How can I achieve dual-DP dual-posture of the sword star of yonghengzhizhizhizhi.com

The three lines corresponding to the character avatar are blood, magic, and DP. The DP line shows your current dp value. This value can be used to gain experience or increase obs by hitting monsters or killing the enemy against the race. The current full value is 4000. Then there are two kinds of drugs in the game, cd for half an hour. After eating, you can increase the DP value by 2000 or 4000 respectively.
The two so-called posture skills that Jianxing can learn in the general skill book need 3000 and 4000 dp values (the corresponding cd time is half an hour and one hour), both of which have an attack bonus, the attack lasted for 30 seconds. If it was launched together, the attack could be improved a lot in a short time. Because the dp-added drug has a cd time, you just need to blame or kill enough of a skill's dp value, then we use a posture, and then immediately take a dp-plus drug and use another posture, which is the so-called dual-dp dual posture.
However, Jianxing also has a superior branding skill assault posture. You only need a magic skill. You can launch it together to achieve three gestures, but this skill will reduce your hit rate by 200, if hit is not enough, we recommend that you use this skill.

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.