zoj 1025 Wooden Sticks 贪心 + 动态规划

                                                                                             Wooden Sticks

 

#include <iostream>
#include <algorithm>
using namespace std;
#define maxN 5005
int n;
struct stick{
	int l;	//木棒的长度
	int w;	//木棒的重量
}a[maxN];

bool cmp(const stick &a, const stick &b){
	if(a.l == b.l)
		return a.w <= b.w;
	return a.l < b.l;
}

//计算重量w的单调递增子序列个数的动态规划实现
int LIS(){
	int i, j;
	int max = 0;
	int b[maxN];
	b[0] = 1;
	for(i = 1; i < n; i++){
		b[i] = 1;
		for(j = 0; j < i; j++){
			if(a[i].w < a[j].w && b[i] == b[j])
				b[i] = b[j] + 1;
		}
		if(b[i] > max)
			max = b[i];
	}
	return max;
}

int main()
{
//	freopen("in.txt","r",stdin);
	int iCase, i;
	cin>>iCase;
	while(iCase--){
		cin>>n;
		for(i = 0; i < n; i++)
			cin>>a[i].l>>a[i].w;
		sort(a, a + n, cmp);
		cout<<LIS()<<endl;
	}
    return 0;
}


 

你可能感兴趣的:(动态规划,ACM,贪心法)