poj-1065 wooden sticks (greedy or DP)

Source: Internet
Author: User
Tags cmath

http://poj.org/problem?id=1065

Test instructions is relatively simple, there are N and sticks, in advance to know the length and width of each stick, these sticks need to be sent to processing, the first stick need a minute of production time, if the length of the current stick and width

is larger than the previous stick, then this stick does not require production time, ask you the minimum production time is how much?

First can be greedy, first by the length of l sort, if L is the same, by the width of W sort.

Starting with i=0, each time the next i+1-n-1 is unmarked and the length and width of the stick is greater than or equal to I, the length and width are marked.

1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 using namespacestd;6 Const intMAXN =5010;7 8 struct Point9 {Ten     intl,w; One     BOOL operator< (ConstPoint &a)Const A     { -         returnL==a.l? w<a.w:l<A.L; -     } the }P[MAXN]; -  - intMARK[MAXN]; -  + intMain () - { +     intT,n; Ascanf"%d",&t); at      while(t--) -     { -scanf"%d",&n); -          for(intI=0; i<n;i++) -         { -scanf"%d%d",&p[i].l,&P[I].W); in         } -Sort (p,p+n); to         //for (int i=0;i<n;i++) printf ("%d%d\n", P[I].L,P[I].W); +         ints=0; -memset (Mark,0,sizeof(Mark)); the          for(intI=0; i<n;i++) *         { $             inttemp=P[I].W;Panax Notoginseng             if(!Mark[i]) -             { the                  for(intj=i+1; j<n;j++) +                 { A                     if(p[j].w>=temp&&!Mark[j]) the                     { +temp=P[J].W; -mark[j]=1; $                     } $                 } -s++; -             } the         } -printf"%d\n", s);Wuyi     } the     return 0; -}

DP: Sort the same as above, then find the longest descending subsequence of W.

1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 using namespacestd;6 Const intMAXN =5010;7 8 struct Point9 {Ten     intl,w; One     BOOL operator< (ConstPoint &a)Const A     { -         returnL==a.l? w<a.w:l<A.L; -     } the }P[MAXN]; -  - intDP[MAXN]; -  + intMain () - { +     intT,n; Ascanf"%d",&t); at      while(t--) -     { -scanf"%d",&n); -          for(intI=0; i<n;i++) -         { -scanf"%d%d",&p[i].l,&P[I].W); in         } -Sort (p,p+n); to         //for (int i=0;i<n;i++) printf ("%d%d\n", P[I].L,P[I].W); +         intans=0; -          for(intI=0; i<n;i++) dp[i]=1; the          for(intI=0; i<n;i++) *         { $              for(intj=0; j<i;j++)Panax Notoginseng                 if(P[J].W&GT;P[I].W) Dp[i]=max (dp[i],dp[j]+1); -ans=Max (ans,dp[i]); the         } +printf"%d\n", ans); A     } the     return 0; +}

poj-1065 wooden sticks (greedy or DP)

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.