[POJ1065] Wooden Sticks

题意:

有N根木棍等待处理。机器在处理第一根木棍时需要准备1分钟,此后遇到长宽都不大于前一根木棍的木棍就不需要时间准备,反之则需要1分钟重新准备。

题解:

dp

题目要求的就是将木棍分成x组,每组木棍的(l_i)(r_i)都是不降的。

要求x最小,则x=将木棍按(l_i)从小到大排序后,(w_i)的最长下降子序列长度L。

根据鸽巢原理,若x<L,则分x组后,必有一组木棍的(w_i)是不降的,与条件不符,所以x==L成立。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
#define N 5010
using namespace std;

int dp[N];
pair<int,int> p[N];

int gi() {
  int x=0,o=1; char ch=getchar();
  while(ch!='-' && (ch<'0' || ch>'9')) ch=getchar();
  if(ch=='-') o=-1,ch=getchar();
  while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
  return o*x;
}

int main() {
  int T=gi(),n,ans;
  while(T--) {
    n=gi(),ans=0;
    for(int i=1; i<=n; i++) {
      p[i].first=gi(),p[i].second=gi();      
    }
    sort(p+1,p+n+1);
    for(int i=1; i<=n; i++) dp[i]=1;
    for(int i=1; i<=n; i++)
      for(int j=1; j<i; j++) {
	if(p[j].second>p[i].second && dp[j]+1>dp[i]) {
	  dp[i]=dp[j]+1;
	}
      }
    for(int i=1; i<=n; i++) {
      ans=max(ans,dp[i]);
    }
    printf("%d
", ans);
  }
  return 0;
}
原文地址:https://www.cnblogs.com/HLXZZ/p/7675122.html