CSU 1225 longest ascending subsequence and records its number

Source: Internet
Author: User
1 For (Int J = 0; j <I; j ++) {2 if (H [I]> H [J]) {3 if (LEN [I] = Len [J] + 1) CNT [I] + = CNT [J]; 4 If (LEN [I] <Len [J] + 1) Len [I] = Len [J] + 1, CNT [I] = CNT [J]; 5} 6 // statistics of the same height 7/* else if (H [I] = H [J]) {8 If (LEN [I] = Len [J]) CNT [I] + = CNT [J]; 9 If (LEN [I] <Len [J]) len [I] = Len [J], CNT [I] = CNT [J]; 10} */11}

Note that if the height is the same, no matter who is standing in the queue, the two are considered only one situation: As shown in the code above, if the statistics are the same, an error will be reported, but the sample can pass through

 

Len [I] indicates the longest sub-sequence length that can be constructed by the first I object.

CNT [I] indicates the number of the maximum length of the largest sequence constructed by the first I object.

 

 1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 #define max(a,b) a>b?a:b 5 const int N = 1005; 6 int h[N] , len[N] , cnt[N]; 7 int main() 8 { 9     freopen("test.in","rb",stdin);10     int n;11     while(scanf("%d",&n)!=EOF){12         memset(len , 0 ,sizeof(len));13         memset(cnt , 0 ,sizeof(cnt));14         len[0] = 1 , cnt[0] = 1;15         for(int i = 0;i < n;i++)16         {17             len[i] = cnt[i] = 1;18             scanf("%d",h+i);19             for(int j=0;j<i;j++){20                 if(h[i] > h[j]){21                     if(len[i] == len[j] + 1) cnt[i]+=cnt[j];22                     if(len[i] < len[j] + 1) len[i] = len[j] + 1 , cnt[i] = cnt[j];23                 }24                 /*else if(h[i] == h[j]){25                     if(len[i] == len[j]) cnt[i] += cnt[j];26                     if(len[i] < len[j]) len[i] = len[j] , cnt[i] = cnt[j];27                 }*/28             }29         }30         int max_len = 0 , cnt_all = 0;31         for(int i=0;i<n;i++){32             max_len = max(max_len , len[i]);33         }34         for(int i=0;i<n;i++){35             if(max_len == len[i])36                 cnt_all += cnt[i];37         }38         printf("%d %d\n",max_len,cnt_all);39     }40     return 0;41 }

 

CSU 1225 longest ascending subsequence and records its number

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.