最近在不忙的时候做做zoj上的题,感觉还是挺好玩的。
下面是对1025这道题的分析。
首先,这道题是一条比较简单的题,核心是求最长非递减子序列的值。
这道题每个case的输入是n个stick的l和w,首先按照l和w进行降序排序(l优先级高于w)。
然后对排序结果求最长非递减序列的长度,就是问题的结果,代码如下。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct stick {
int l, w;
} stick;
vector<stick> s;
int n;
int comp(const stick &s1, const stick &s2)
{
if (s1.l == s2.l) return s1.w > s2.w;
return s1.l > s2.l;
}
int mds()
{
int i, j, max, k, b[5000];
for(i=0, max=0;i<n;i++) {
k = 0;
for(j=0;j<i;j++) {
if (s[i].w > s[j].w && b[j] > k) k = b[j];
}
b[i] = k+1;
if (b[i] > max) max = b[i];
}
return max;
}
int main()
{
int t, i;
stick tmp;
cin >>t;
while(t--) {
s.clear();
cin>>n;
for(i=0;i<n;i++) {
cin >>tmp.l>>tmp.w;
s.push_back(tmp);
}
sort(s.begin(), s.end(), comp);
cout<<mds()<<endl;
}
return 0;
}