Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

离散化,记忆化搜索过

Posted by qianlv at 2013-03-01 21:06:49 on Problem 2241
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define CL(a,b) memset(a,b,sizeof(a))
#define MAX(a,b) (a>b?a:b)
using namespace std;
const int N(200);
struct data{
	int x,y,z;
}a[N<<3];
bool  epual (data one,data two)
{
	if(one.x!=two.x||one.y!=two.y||one.z!=two.z)
		return true;
	return false;
}
int arr[N<<3];
void change(int i,int &j)
{
	a[j].z=a[i].x;a[j].x=a[i].y;a[j++].y=a[i].z;
	a[j].z=a[i].x;a[j].x=a[i].z;a[j++].y=a[i].y;
	a[j].z=a[i].y;a[j].x=a[i].x;a[j++].y=a[i].z;
	a[j].z=a[i].y;a[j].x=a[i].z;a[j++].y=a[i].x;
	a[j].z=a[i].z;a[j].x=a[i].y;a[j++].y=a[i].x;
}
int fkey(int val,int l,int r)
{
	int mid;
	while(l<=r)
	{
		mid=(l+r)>>1;
		if(arr[mid]==val)
			return mid;
		else if(val<arr[mid])
			r=mid-1;
		else
			l=mid+1;
	}
	return -1;
}
bool cmp(data one,data two)
{
	return one.x<two.x||(one.x==two.x&&one.y<two.y)||(one.x==two.x&&one.y==two.y&&one.z<two.z);
}
int dp[N][N];
bool vis[N<<3];
void dfs(int t,int px,int py)
{
	int i;
	for(i=0;i<t;i++)
		if(!vis[i]&&dp[px][py]+a[i].z>dp[a[i].x][a[i].y]&&(px>a[i].x&&py>a[i].y))
		{
			vis[i]=true;
			dp[a[i].x][a[i].y]=dp[px][py]+a[i].z;
			dfs(t,a[i].x,a[i].y);
			vis[i]=false;
		}
	return;
}
int main()
{
	int n,i,m,j,k,cas=1;
	while(scanf("%d",&n)&&n)
	{
		for(i=0,j=0;i<n;i++)
		{
			scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z);
			arr[j++]=a[i].x;
			arr[j++]=a[i].y;
			arr[j++]=a[i].z;
		}
		sort(arr,arr+j);
		m=1;
		for(i=1;i<j;i++)
			if(arr[i]!=arr[i-1]) arr[m++]=arr[i];
		//for(i=0;i<m;i++)
		//	printf("%d ",arr[i]);
		j=n;
		for(i=0;i<n;i++)
			change(i,j);
		sort(a,a+j,cmp);
		int t=1;
		for(i=1;i<j;i++)
			if(epual(a[i],a[i-1]))
				a[t++]=a[i];
		for(i=0;i<t;i++)
		{
			a[i].x=fkey(a[i].x,0,m-1);
			a[i].y=fkey(a[i].y,0,m-1);
		}
		//for(i=0;i<t;i++)
		//	printf("%d %d %d\n",a[i].x,a[i].y,a[i].z);
		CL(dp,0);
		dfs(t,m,m);
		int sum=0;
		for(i=0;i<m;i++)
			for(j=0;j<m;j++)
				sum=MAX(sum,dp[i][j]);
		printf("Case %d: maximum height = %d\n",cas++,sum);

	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator