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 church at 2007-05-18 17:41:52 on Problem 1015
#include <iostream.h>
#define k 500

bool a[1000],v[1000][25];
int b[1000][25],sum[1000][25];
int d[210],p[210],c[210],e[210],play[210];
int n,m,dmax,pmax;

void sort(int h,int l);
void done(void);
void work(int pp[210]);
void print(int num,int step);

void main()
{
	int i,j,step,h;
	step=0;
	cin>>n>>m;
	while (n>0)
	{
		step++;
		cout<<"Jury #"<<step<<endl;
		for(i=1;i<=n;i++)
		{
			play[i]=i;
			cin>>d[i]>>p[i];
			c[i]=d[i]-p[i];
		}
		for(i=1;i<=n;i++)
			e[i]=d[i]+p[i];
		sort(1,n);
		for(i=1;i<=n;i++)
			b[i][0]=play[i];
		done();
		work(play);
		cout<<"Best jury has value "<<pmax<<" for prosecution and value "<<dmax<<" for defence:"<<endl;
        for(i=1;i<m;i++)
			for(j=i+1;j<=m;j++)
				if (e[j]<e[i])
				{
					h=e[j]; e[j]=e[i]; e[i]=h;
				}

		for(i=1;i<=m;i++)
			cout<<" "<<e[i];
		cout<<endl;
		cout<<endl;
		cin>>n>>m;
	}
}


void sort(int h,int l)
{
	int i,j,mid,g;
	i=h; j=l;
	mid=c[(i+j)/2];
	do
	{
		while ((c[i]<mid) && (i<l)) i++;
		while ((c[j]>mid) && (j>h)) j--;
		if (i<=j)
		{
			g=c[i]; c[i]=c[j]; c[j]=g;
			g=e[i]; e[i]=e[j]; e[j]=g;
			g=play[i]; play[i]=play[j]; play[j]=g;
			i++; j--;
		}
	}while (i<=j);
	if (i<l) sort(i,l);
	if (j>h) sort(h,j);
}


void done(void)
{
   int i,j,l,h,num,u,min,max;
   for(i=100;i<=1000;i++)
   {
	   a[i]=false;
	   for(j=1;j<=23;j++)
	   {
		   sum[i][j]=0;
		   v[i][j]=false;
	   }
   }
   num=c[1]+k;
   b[num][1]=1;
   v[num][1]=true;
   sum[num][1]+=e[1];
   a[num]=true;
   h=num;  l=num; min=num; max=num;
   for(i=2;i<=n;i++)
   {
       for(j=h;j<=l;j++)
		   if (a[j])
		   {
              if (j+c[i]<min)
				  min=j+c[i];
			  if (j+c[i]>max)
				  max=j+c[i];
			  	   for(u=1;u<m;u++)
					   if ((v[j][u]) && (!v[j+c[i]][u+1]))
					   {
						   v[j+c[i]][u+1]=true;
						   b[j+c[i]][u+1]=i;
						   sum[j+c[i]][u+1]=sum[j][u]+e[i];
					   }
					   else
						   if ((v[j][u]) && (v[j+c[i]][u+1]) && (sum[j][u]+e[i]>sum[j+c[i]][u+1]))
						   {
							   b[j+c[i]][u+1]=i;
							   sum[j+c[i]][u+1]=sum[j][u]+e[i];
						   }
		   }
		   if (c[i]<0)
               for(j=h;j<=l;j++)
		          if (a[j]) 
			        a[j+c[i]]=true;
		   if (c[i]>0)
			   for(j=l;j>=h;j--)
				   if (a[j])
					   a[j+c[i]]=true;
			num=c[i]+k;
			if (num>max)
				max=num;
			if (num<min)
				min=num;
			a[num]=true;
			if (((v[num][1]) && (sum[num][1]<e[i])) || (!v[num][1]))
			{
				v[num][1]=true;
			    sum[num][1]=e[i];
			    b[num][1]=i;
			}
			h=min; l=max;
			if (h<100)
				h=100;
			if (l>800)
				l=800;
	}
 
}
		
void work(int pp[210])
{
	int i;
	for(i=0;i<401;i++)
		if ((v[k+i][m]) || (v[k-i][m]))
			break;
		if (sum[k+i][m]>sum[k-i][m])
			print(k+i,1);
		else
			print(k-i,1);
		pmax=0; dmax=0;
		for(i=1;i<=m;i++)
		{
			e[i]=b[e[i]][0];
			pmax+=p[e[i]];
			dmax+=d[e[i]];
		}
}

void print(int num,int step)
{
	if (step<=m)
	{
	e[step]=b[num][m-step+1];
	num=num-c[e[step]];
	print(num,step+1);
	}
}

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