Usaco buy low, buy Lower

Source: Internet
Author: User

This question has two questions, the first question is the longest descending subsequence.

For the second ask the number of the longest descending sequence can be solved by asking the first question process. Set Maxcnt[i] to be the number of the longest descending sequence at the end of item I.

For all J (1≤j≤i-1) if there is (S[j]>s[i] and maxlength[j]+1>maxlength[i]) then maxcnt[i]=maxcnt[j], otherwise if (maxlength[j]+1= = Maxlength[i]) can take advantage of the addition principle, maxcnt[i]=maxcnt[i]+maxcnt[j].

Taking into account the problem can not repeat the sequence, we can add a domain next[i] is greater than I and the nearest I next[i] so that the number of next[i] is the same as the number of first. If there is no such number then next[i]=0. This way we are in DP when the NEXT[J] is not 0 and Next[j]<i can skip directly.

/*id:modengd1prob:buylowlang:c++*/#include <iostream> #include <stdio.h> #include <memory.h># Include <vector>using namespace Std;int n;long input[5001];int dp[5001],next[5001];int c[5001][500];//    maxcnt array int main () {freopen ("buylow.in", "R", stdin);    Freopen ("Buylow.out", "w", stdout);    scanf ("%d", &n);    for (int i=0;i<n;i++) scanf ("%ld", &input[i]);    input[n]=0;    Memset (Dp,0,sizeof (DP));                for (int i=0;i<=n;i++) {for (int j=0;j<i;j++) {if (Input[j]>input[i]) {            Dp[i]=max (Dp[i],dp[j]);    }} dp[i]++;    } memset (Next,0,sizeof (next));                for (int i=0;i<=n;i++) {for (int j=i+1;j<=n;j++) {if (Input[i]==input[j]) {                Next[i]=j;            Break    }}} memset (C,0,sizeof (c)); for (int i=0;i<=n;i++) {for (int j=0;j<i;j++) {if (next[j]==0| | Next[j]>i) &&dp[j]==dp[i]-1&&input[j]>input[i]) {int k; for (k=1;k<=c[j][0];k++) {c[i][k]+=c[j][k];c[i][k+1]+=c[i][k]/10;c[i][k]%=10;} while (c[i][k]>=10) {c[i][k+1]+=c[i][k]/10;c[i][k]%=10;k++;}            while (c[i][k]==0) k--;if (k>=c[i][0]) c[i][0]=k;            }} if (c[i][0]==0) {c[i][0]=1;        C[i][1]=1;    }} cout<<dp[n]-1<< ';    for (int i=c[n][0];i>0;i--) cout<<c[n][i];    cout<<endl; return 0;}

  

Usaco buy low, buy Lower

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.