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

5000+ms过了,附代码,用上快排和线表保存得到结果就可以避免超时了

Posted by 596380446 at 2014-11-04 17:40:17 on Problem 1186
#include<stdio.h>
#include<stdlib.h>
//
//int用交换函数
//
void SwapInt(int *pInt,int pos1,int pos2)
{
	int temp=pInt[pos1];
	pInt[pos1]=pInt[pos2];
	pInt[pos2]=temp;
}
//
//int用快速排序
//
void QuickSortInt(int *pInt,int start,int stop)
{
	char *cdt="L";
	int left=start,right=stop-1;
	while(left!=right)
	{
		if(cdt=="L")
		{
			if(pInt[left]>pInt[right])
			{
				SwapInt(pInt,left,right);
				left++;
				cdt="R";
			}
			else
			{
				right--;
			}
		}
		else if(cdt=="R")
		{
			if(pInt[right]<pInt[left])
			{
				SwapInt(pInt,left,right);
				right--;
				cdt="L";
			}
			else
			{
				left++;
			}
		}
	}
	if(start<left) QuickSortInt(pInt,start,left);
	if(left+1<stop) QuickSortInt(pInt,left+1,stop);
}
//
//定义x的数目,最大值,系数和指数,两边的所得值,值长度
//
int n,m;
int k[6],p[6];
int valueL[10000000],valueR[10000000];
int lengthL,lengthR;
//
//定义int用pow函数
//
int powInt(int base,int time)
{
	int sum=1;
	for(int i=0;i<time;i++)
	{
		sum*=base;
	}
	return sum;
}
//
//递归函数
//
int Rec(int start,int stop,int value,char *way)
{
	//如果到达终点
	if(start==stop)
	{
		if(way=="L")
		{
			valueL[lengthL++]=value;
		}
		else if(way=="R")
		{
			valueR[lengthR++]=(-1)*value;
		}
	}
	//如果还未到达终点
	else
	{
		for(int i=1;i<=m;i++)
		{
			Rec(start+1,stop,value+k[start]*powInt(i,p[start]),way);
		}
	}
	return 0;
}
//
//main函数
//
int main()
{
	//初始化长度
	lengthL=lengthR=0;
	//读取内容
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
	{
		scanf("%d%d",k+i,p+i);
	}
	//调用递归函数
	Rec(0,n/2,0,"L");
	Rec(n/2,n,0,"R");
	//快速排序
	QuickSortInt(valueL,0,lengthL);
	QuickSortInt(valueR,0,lengthR);
	//获取valueL和valueR中相等的发生次数
	int sum=0;
	int timeL=1,timeR=1,left=0,right=0;
	for(timeL=1;left+1<lengthL&&valueL[left]==valueL[left+1];timeL++,left++) ;
	for(timeR=1;right+1<lengthR&&valueR[right]==valueR[right+1];timeR++,right++) ;
	while(left<lengthL&&right<lengthR)
	{
		if(valueL[left]<valueR[right])
		{
			left++;
			for(timeL=1;left+1<lengthL&&valueL[left]==valueL[left+1];timeL++,left++) ;
		}
		else if(valueL[left]>valueR[right])
		{
			right++;
			for(timeR=1;right+1<lengthR&&valueR[right]==valueR[right+1];timeR++,right++) ;
		}
		else if(valueL[left]==valueR[right])
		{
			sum+=timeL*timeR;
			left++,right++;
			for(timeL=1;left+1<lengthL&&valueL[left]==valueL[left+1];timeL++,left++) ;
			for(timeR=1;right+1<lengthR&&valueR[right]==valueR[right+1];timeR++,right++) ;
		}
	}
	//输出结果后结束
	printf("%d\n",sum);
	system("pause");
	return 0;
}

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