2011ACM上海邀请赛J题( Juice Extractor)----离散化+DP

题目连接:http://acm.hust.edu.cn:8080/judge/problem/viewProblem.action?id=19242

 

PS.这题最后想到思路了,但是时间不够了、、、哎

 

CODE:

/*离散化+DP*/
/*AC代码:372ms*/
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <cstdio>
#include <cstdlib>
#include <set>
#define max(a,b) (a>b?a:b)
#define MAXN 1005
using namespace std;
struct Node
{
	int x,y;
}node[MAXN];
int dp[MAXN],b[2*MAXN];
set<int>S;
set<int>::iterator itor;
int N,cas,cnt;
int cmp(const void *p1,const void *p2)
{
	if(((struct Node *)p1)->x!=((struct Node *)p2)->x)
		return ((struct Node *)p1)->x-((struct Node *)p2)->x;
	return ((struct Node *)p1)->y-((struct Node *)p2)->y;
}
void Init()
{
	int i;
	scanf("%d",&N);
	S.clear();
	for(i=1;i<=N;i++)
	{
		scanf("%d%d",&node[i].x,&node[i].y);
		S.insert(node[i].x);
		S.insert(node[i].y);
	}
	qsort(node+1,N,sizeof(node[0]),cmp);
	//离散化
	for(cnt=0,itor=S.begin();itor!=S.end();cnt++,itor++)
		b[cnt]=*itor;
}
void Solve()
{
	int i,j,k,ans=0;
	for(i=0;i<cnt;i++)
	{
		int num=0;
		for(j=1;j<=N;j++)
		{
			if(node[j].x<=b[i]&&node[j].y>=b[i])
				num++;
		}
		if(num<=2) num=0;
		dp[i]=num;
		k=1;
		//DP
		for(j=0;j<i;j++)
		{
			while(k<=N&&node[k].x<=b[j])
			{
				if(node[k].y>=b[i]) num--;
				k++;
			}
			if(num<=2) num=0;
			dp[i]=max(dp[i],dp[j]+num);
		}
		ans=max(ans,dp[i]);
	}
	printf("Case #%d: %d\n",cas++,ans);
}
int main()
{
	int T;
	cas=1;
	scanf("%d",&T);
	while(T--)
	{
		Init();
		Solve();
	}
	return 0;
}

你可能感兴趣的:(struct,iterator)