POJ 3670 Eating Together (①o (n) longest field and)

Source: Internet
Author: User

To find the number of numbers in the queue that do not conform to the non-ascending (descending) Order trend,

Determine the number of ascending and descending order, and finally take the minimum.

Two methods:

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>using namespace STD;#define MAXN 30005intNintCOW[MAXN];intf[maxn][5];intMain () {scanf("%d", &n); for(inti =1; I <= N; i++)scanf("%d", &cow[i]);memset(F,0,sizeof(f)); for(inti =1; I <= N; i++) { for(intj =1; J <=3; J + +) {F[i][j] = f[i-1][1]; for(intK =2; K <= J; k++) F[i][j] = min (f[i][j], f[i-1][k]);if(Cow[i]! = j) f[i][j]++; }    }intans = n; for(inti =1; I <=3; i++) ans = min (ans, f[n][i]); for(inti =1; I <= N; i++) { for(intj =1; J <=3; J + +) {F[i][j] = f[i-1][3]; for(intK =2; K >= J; k--) F[i][j] = min (f[i][j], f[i-1][k]);if(Cow[i]! = j) f[i][j]++; }    } for(inti =1; I <=3; i++) ans = min (ans, f[n][i]);printf("%d\n", ans);return 0;}
#include <iostream>#include <algorithm>#include <cstdio>using namespace STD;Const intsup=30005;intLds[sup];//Non-risingintLis[sup];//Do not dropintDp[sup];intNintBsearch (intBegintEndintP) {intMid while(Beg<=end) {mid= (Beg&end) + ((beg^end) >>1);if(dp[mid]<=p) beg=mid+1;Else if(dp[mid]>p) end=mid-1; }returnBeg;}intLis () {intIinttop=-1; for(i=0; i<n;++i) {if(dp[top==-1?0: Top]<=lis[i]) dp[++top]=lis[i];Else if(dp[top==-1?0: Top]>lis[i]) Dp[bsearch (0, Top,lis[i])]=lis[i]; }returnn-top-1;}intlds () {inti,top=-1; for(i=0; i<n;++i) {if(dp[top==-1?0: Top]<=lds[i]) dp[++top]=lds[i];Else if(dp[top==-1?0: Top]>lds[i]) Dp[bsearch (0, Top,lds[i])]=lds[i]; }returnn-top-1;}intMain () {intIscanf("%d", &n); for(i=0; i<n;++i) {scanf("%d", &lis[i]); lds[n-i-1]=lis[i]; }intAns=lis (); Fill (Dp,dp+sup,0);intT=lds (); ans=ans<t?ans:t;if(ans!=-1)printf("%d\n", ans);Else        printf("0\n");return 0;}

POJ 3670 Eating Together (①o (n) longest field and)

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.