【洛谷1223】木棍加工

原题:

 n<=5000

结论题 = =

dilworth定理,对于一个偏序集,最少链划分等于最长反链长度

即序列的最少下降划分等于最长不下降子序列长度

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 using namespace std;
 5 struct nds{int x,y;}a[5100];
 6 int n;
 7 int f[5100];
 8 bool cmp(nds x,nds y){  return x.x==y.x ? x.y<y.y : x.x<y.x;}
 9 int main(){
10     cin>>n;
11     for(int i=1;i<=n;++i)  scanf("%d%d",&a[i].x,&a[i].y);
12     sort(a+1,a+n+1,cmp);
13     for(int i=1;i<=n;++i){
14         f[i]=1;
15         for(int j=1;j<i;++j)if(a[j].y>a[i].y)
16             f[i]=max(f[i],f[j]+1);
17     }
18     int ans=0;
19     for(int i=1;i<=n;++i)  ans=max(ans,f[i]);
20     cout<<ans<<endl;
21     return 0;
22 }
View Code

猜你喜欢

转载自www.cnblogs.com/cdcq/p/12283758.html
今日推荐