标签:
n根木材长l_i重w_i,前一根木材大于后一根的话要浪费一分钟准备机器,求最省方案?
我们把问题抽象出来,那就是:把一个数列划分成最少的最长不升子序列
Dilworth定理:对于一个偏序集,最少链划分等于最长反链长度。
这个问题是二元组的最少链划分,那么我们以a[]为关键字大小进行排序,如果a[]中相同就按照b[]排序,根据
Dilworth定理,然后题目就变成了求b[]序列中最长严格下降子序列长度了。
1 #include <iostream> 2 #include <algorithm> 3 #include <functional> 4 using namespace std; 5 6 pair<int, int> stick[5000 + 16]; 7 int dp[5000 + 16]; 8 int main() 9 { 10 int T; 11 cin >> T; 12 while (T--) 13 { 14 int n; 15 cin >> n; 16 for (int i = 0; i < n; ++i) 17 { 18 cin >> stick[i].first >> stick[i].second; 19 } 20 sort(stick, stick + n); 21 memset(dp, -1, n * sizeof(int)); 22 for (int j = 0; j < n; ++j) 23 { 24 //最长下降序列长度求法 25 *lower_bound(dp, dp + n, stick[j].second, greater<int>()) = stick[j].second; 26 } 27 cout << lower_bound(dp, dp + n, -1,greater<int>()) - dp << endl; 28 } 29 return 0; 30 }
标签:
原文地址:http://www.cnblogs.com/xlsryj/p/4768401.html