HDU 4293 Groups(区间DP)

/*
具体思路参见:http://hi.baidu.com/dispossessed/item/320544233e27109b9c63d1a6
将每组数据转换为区间以后,然后按照x从小到大排序,便可以用DP来做。注意不符合条件的排除,①处的作用
*/

#include <cstdio>
#include <cstdlib>
#include <cstring>
const int nMax = 507;
int d[nMax];
int N;
struct Node
{
	int x, y;
	int cnt;
}node[nMax];

int cmp(const void *a, const void *b)
{
	Node *pa = (Node *) a;
	Node *pb = (Node *) b;
	return pa ->x - pb ->x;
}

int max(int a, int b)
{
	return a > b ? a : b;
}

int main()
{
	//freopen("e://data.in", "r", stdin);
	while(scanf("%d", &N) != EOF)
	{
		int i, j;
		for(i = 0; i < N; ++ i)
		{
			scanf("%d%d", &node[i].x, &node[i].y);
			node[i].cnt = 1;
		}
		qsort(node, N, sizeof(node[0]), cmp);
		for(i = 1, j = 0; i < N; ++ i)
		{
			if(node[j].x == node[i].x && node[j].y == node[i].y)
				node[j].cnt ++;
			else
			{
				if(node[j].cnt > (N - node[j].x - node[j].y))//①
					node[j].cnt = N - node[j].x - node[j].y;
				j ++;
				node[j].x = node[i].x;
				node[j].y = node[i].y;
			}
		}
		if(node[j].cnt > (N - node[j].x - node[j].y))
			node[j].cnt = N - node[j].x - node[j].y;
		memset(d, 0, sizeof(d));
		int up = j;
		for(i = 0; i <= up; ++ i)
		{
			for(j = node[i].x; j >= 0; j --)
				d[N - node[i].y] = max(d[N - node[i].y], d[j] + node[i].cnt);
		}
		int _max = 0;
		for(i = 0; i <= N; ++ i)
			if(d[i] > _max)
				_max = d[i];
		printf("%d\n", _max);
	}
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值