HDU 1050 Moving Tables (贪心 区间最大叠加数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1050

参考链接:http://blog.csdn.net/code_pang/article/details/8251240

思路1:贪心

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))

struct Point
{
    int s,e;
    bool vis;
    void get ()
    {
        scanf("%d%d",&s,&e);
		int small=(min(s,e)+1)/2;    //此处注意
		int big=(max(s,e)+1)/2;
		s=small;
		e=big;
		vis=false;
    }

    bool operator < (const Point& _P) const   //第一关键字:开始时间
    {
        return s<_P.s||(s==_P.s&&e<_P.e);
    };
}data[205],temp;
int n;

int Find (int start)   //寻找从data[start]开始的第一个没有访问过的点
{
	for (int i=start;i<n;i++)
	    if (data[i].vis==false)
	        return i;
	return n;
}

int main ()
{
	int T;
	scanf("%d",&T);
	while (T--)
	{
		int i,num=0,ans=0;
		scanf("%d",&n);
		for (i=0;i<n;i++)
			data[i].get();
		sort(data,data+n);
		while (num<n)
		{
			int first=Find(0);
			temp=data[first];
			data[first].vis=true;
			num++;
			for (i=Find(first);i<n;i++)
				if (data[i].s>temp.e && data[i].vis==false)
				{
					num++;
					temp=data[i];
					data[i].vis=true;
				}
			ans++;
		}
		printf("%d\n",ans*10);
	}
	return 0;
}

/*
1 
4
10 5
1 4
8 9
3 6

Out
20
*/

思路2:区间最大叠加数

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))

int main ()
{
	int T,n,cnt[205];
	scanf("%d",&T);
	while (T--)
	{
		int i,s,e;
		memset(cnt,0,sizeof(cnt));
		scanf("%d",&n);
		for (i=0;i<n;i++)
		{
			scanf("%d%d",&s,&e);
			int small=(min(s,e)+1)/2;    //此处注意  
			int big=(max(s,e)+1)/2;
			s=small;
			e=big;
			for (int j=s;j<=e;j++)
				cnt[j]++;
		}
		int ans=-1;
		for (i=0;i<205;i++)
			if (ans<cnt[i])
				ans=cnt[i];
		printf("%d\n",ans*10);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值