caioj 1083 动态规划入门(非常规DP7:零件分组)(LIS)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34416123/article/details/82079378

这道题题目给的顺序不是固定的
所以一开始要自己排序,按照w来排序
后来只要看l就可以了
然后求最长下降子序列即可(根据那个神奇的定理,LIS模板里有提到)
 

#include<cstdio>
#include<algorithm>
#include<cstring>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;

const int MAXN = 1123;
struct node
{
	int w, l;
	bool operator < (const node& rhs) const
	{
		return l > rhs.l;
	}
}a[MAXN], f[MAXN];

bool cmp(node a, node b)
{
	return a.w < b.w || ((a.w == b.w) && a.l < b.l);
}

int main()
{
	int n;
	scanf("%d", &n);
	REP(i, 0, n) 
		scanf("%d%d", &a[i].l, &a[i].w);
	sort(a, a + n, cmp);
	
	int len = 1;
	f[len] = a[0];
	REP(i, 1, n)
	{
		if(f[len] < a[i]) f[++len] = a[i];
		else f[lower_bound(f + 1, f + len + 1, a[i]) - f] = a[i];
	}
	printf("%d\n", len);

	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_34416123/article/details/82079378
今日推荐