题目链接: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;
}