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

内存占用如何能少点,AC代码

Posted by 11115122 at 2017-03-13 20:40:59 on Problem 2397
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
int theMinHeigh[1000][1000];//一个数组 数组中(i,h)保存经过 i步到达 h 高度中 最高点的最小值,若无法到达赋值为-1
bool UpOrDown[1000][1000];//一个标记数组,取值 向上  或  向下
//该函数实现问题求解
string GetResult(int a[],int size)
{
	
	for (int i = 0; i < 1000; i++)
		for (int j = 0; j < 1000; j++)
			theMinHeigh[i][j] = -1;
	theMinHeigh[0][0] = 0;//初始化为0
	for(int NumOfSteps=1;NumOfSteps<=size;NumOfSteps++)  ///外层循环由 步数控制
		for (int heigh = 0; heigh < 1000; heigh++)
		{
			int Num1 = -1;
			int Num2 = -1;
			if (heigh - a[NumOfSteps - 1] >= 0)
			{
				if (theMinHeigh[NumOfSteps - 1][heigh - a[NumOfSteps - 1]] != -1)  //判断前一步能否到达 heigh-m高度  m为当前步数移动高度
				{
					//到达当前位置路途所经过的最高点,在以下2个中取较大值
					Num1 = max(theMinHeigh[NumOfSteps - 1][heigh - a[NumOfSteps - 1]], heigh);
				}
			}
			if (heigh + a[NumOfSteps - 1] <= 1000)
			{
				if (theMinHeigh[NumOfSteps - 1][heigh + a[NumOfSteps - 1]] != -1)
				{
					//前一步到达 heigh+M位置,那么向下爬m到达当前位置,路途中经过的最高点 与前一步一致
					Num2 = theMinHeigh[NumOfSteps - 1][heigh + a[NumOfSteps - 1]];
				}
			}
			//到达某一高度,最多可由2种前一步向下或向上到达,分析2者最高点,取其较小值
			if (Num1 == -1 && Num2 == -1)
				theMinHeigh[NumOfSteps][heigh] = -1;//表示无法经过这么多步到达改点
			if (Num1 == -1 && Num2 != -1)
			{
				//仅由前一步向下移动而来
				theMinHeigh[NumOfSteps][heigh] = Num2;
				UpOrDown[NumOfSteps][heigh] = false;
			}
			if (Num1 != -1 && Num2 == -1)
			{
				//经由前一步向上移动而来
				theMinHeigh[NumOfSteps][heigh] = Num1;
				UpOrDown[NumOfSteps][heigh] = true;
			}
			if (Num1 != -1 && Num2 != -1)

			{
				//若可由前一步向上,或向下而来。取其较小者
				if (Num1 < Num2)
				{
					theMinHeigh[NumOfSteps][heigh] = Num1;
					UpOrDown[NumOfSteps][heigh] = true;
				}
				else
				{
					theMinHeigh[NumOfSteps][heigh] = Num2;
					UpOrDown[NumOfSteps][heigh] = false;
				}
			}
		}



	//循环结束  数组  (n,0)位置表示经过n步重新回到0位置,途中经过最高点的最小值
	string result;
	if (theMinHeigh[size][0] == -1)
	{
		result = "IMPOSSIBLE";
		return result;
	}
	//依次从最后一个往前读取出移动方向
	int theheigh = 0;
	for (int step = size; step >= 1; step--)
	{
		//若由前一步向下移动而来,前一步高度  theheigh+=a[i];
		if (UpOrDown[step][theheigh] == false)
		{
			result += "D";
			theheigh += a[step-1];
		}
		else
		{
			result += "U";
			theheigh -= a[step-1];
		}

	}
	string msg;
	for (int i = 0; i < result.size(); i++)
		msg += result[result.size() - i-1];
	return msg;


}
int main()
{
	int testDatas;
	cin >> testDatas;
	vector<string>  results;
	for (int i = 0; i < testDatas; i++)
	{
		int size;
		cin >> size;
		int *datas = new int[size];
		for (int j = 0; j < size; j++)
		{
			cin >> datas[j];
		}
		results.push_back(GetResult(datas, size));
		delete[]datas;
	}
	for (int i = 0; i < results.size(); i++)
		cout << results[i] << endl;
	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