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 liyan199311 at 2017-02-27 15:48:41 on Problem 1009
代码很水,应该可以看看

#include<iostream>
#include<vector>
#include<cstdlib>
#include<math.h>
#include<time.h>
#include<algorithm>
using namespace std;

void handleTask(int v_index[1000], int width, int weight[1001], int length);
int getValue(int v[1000], int weight[1001], int pos);

class outMap
{
public :
	int pos;
	int value;
};

int getMaxValue(int v_index[1000], int weight[1001], int width, int pos, bool is_one_raw, int length)
{
	int value_1 = 0, value_2 = 0, value_3 = 0, value_4 = 0, value_6 = 0, value_7 = 0, value_8 = 0, value_9 = 0;
	int value = getValue(v_index, weight, pos);
	int index = 0;
	if (is_one_raw)
	{
		if (pos% width == 0)
		{
			value_6 = abs(getValue(v_index, weight, pos + 1) - value);
		}
		else if (pos% width == width - 1)
		{
			value_4 = abs(getValue(v_index, weight, pos - 1) - value);
		}
		else
		{
			value_4 = abs(getValue(v_index, weight, pos - 1) - value);
			value_6 = abs(getValue(v_index, weight, pos + 1) - value);
		}
	}
	else if (pos < width)
	{
		value_8 = abs(getValue(v_index, weight, pos + width) - value);
		if (pos < width - 1)
		{
			value_6 = abs(getValue(v_index, weight, pos + 1) - value);
			value_9 = abs(getValue(v_index, weight, pos + width + 1) - value);
		}
		if (pos > 0)
		{
			value_4 = abs(getValue(v_index, weight, pos - 1) - value);
			value_7 = abs(getValue(v_index, weight, pos + width - 1) - value);
		}
	}
	else if (pos >= weight[length] - width)
	{
		value_2 = abs(getValue(v_index, weight, pos - width) - value);
		if (pos % width < width - 1)
		{
			value_6 = abs(getValue(v_index, weight, pos + 1) - value);
			value_3 = abs(getValue(v_index, weight, pos - width + 1) - value);
		}
		if (pos % width > 0)
		{
			value_4 = abs(getValue(v_index, weight, pos - 1) - value);
			value_1 = abs(getValue(v_index, weight, pos - width - 1) - value);
		}
	}
	else
	{
		value_2 = abs(getValue(v_index, weight, pos - width) - value);
		value_8 = abs(getValue(v_index, weight, pos + width) - value);
		if (pos % width <width - 1)
		{
			value_6 = abs(getValue(v_index, weight, pos + 1) - value);
			value_3 = abs(getValue(v_index, weight, pos - width + 1) - value);
			value_9 = abs(getValue(v_index, weight, pos + width + 1) - value);
		}
		if (pos % width > 0)
		{
			value_4 = abs(getValue(v_index, weight, pos - 1) - value);
			value_1 = abs(getValue(v_index, weight, pos - width - 1) - value);
			value_7 = abs(getValue(v_index, weight, pos + width - 1) - value);
		}
	}
	int max_value = value_1;
	max_value = max_value > value_2 ? max_value : value_2;
	max_value = max_value > value_3 ? max_value : value_3;
	max_value = max_value > value_4 ? max_value : value_4;
	max_value = max_value > value_6 ? max_value : value_6;
	max_value = max_value > value_7 ? max_value : value_7;
	max_value = max_value > value_8 ? max_value : value_8;
	max_value = max_value > value_9 ? max_value : value_9;
	return max_value;
}

int getValue(int v[1000], int weight[1001], int pos)
{
	int index = 1;
	int value = 0;
	while (weight[index] <= pos)
	{
		index++;
	}
	return v[index - 1];
}

int compare(const void* l, const void* r)
{
	outMap* ll = (outMap*)l;
	outMap* rr = (outMap*)r;
	return (*ll).pos - (*rr).pos;
}

int main()
{
	int width = 0;
	while (cin >> width)
	{
		int v_value[1000];
		int weight[1001], length = 0;
		if (width == 0)
		{
			break;
		}
		int temp_value = 0, temp_size = 0;
		long max_length = 0;
		weight[0] = max_length;
		while (cin >> temp_value >> temp_size)
		{
			if (temp_value == 0 && temp_size == 0)
			{
				break;
			}
			max_length += temp_size;
			v_value[length] = temp_value;
			weight[length + 1] = max_length;
			length++;
		}
		handleTask(v_value, width, weight, length);
	}
	cout << 0 << endl;
	system("pause");
	return 0;
}

void handleTask(int v_index[1000], int width, int weight[1001], int length)
{
	outMap returnResult[9001];
	int pos = 0;
	int index = 0;
	bool one_raw = weight[length] <= width;
	int last_raw_index = weight[length] - width;
	outMap outMap_0;
	outMap_0.pos = 0;
	outMap_0.value = getMaxValue(v_index, weight, width, 0, one_raw, length);
	returnResult[index++] = outMap_0;
	for (int i = 0; i <= length; i++)
	{
		outMap outData_1, outData_2, outData_3, outData_4, outData_6, outData_7, outData_8, outData_9;
		int pos = weight[i];

		if (pos > 0)
		{
			outData_4.pos = pos - 1;
			outData_4.value = getMaxValue(v_index, weight, width, pos - 1, one_raw, length);
			returnResult[index++] = outData_4;
		}
		if (pos < weight[length] - 1)
		{
			outData_6.pos = pos + 1;
			outData_6.value = getMaxValue(v_index, weight, width, pos + 1, one_raw, length);
			returnResult[index++] = outData_6;
		}

		if (pos > width - 1)
		{
			outData_2.pos = pos - width;
			outData_2.value = getMaxValue(v_index, weight, width, pos - width, one_raw, length);
			returnResult[index++] = outData_2;
		}
		if (pos > width)
		{
			outData_1.pos = pos - width - 1;
			outData_1.value = getMaxValue(v_index, weight, width, pos - width - 1, one_raw, length);
			returnResult[index++] = outData_1;
		}
		if (pos > width + 1 && pos < weight[length] +width - 1)
		{
			outData_3.pos = pos - width + 1;
			outData_3.value = getMaxValue(v_index, weight, width, pos - width + 1, one_raw, length);
			returnResult[index++] = outData_3;
		}
		if (pos < last_raw_index)
		{
			outData_8.pos = pos + width;
			outData_8.value = getMaxValue(v_index, weight, width, pos + width, one_raw, length);
			returnResult[index++] = outData_8;
		}
		if (pos < last_raw_index - 1)
		{
			outData_7.pos = pos + width - 1;
			outData_7.value = getMaxValue(v_index, weight, width, pos + width - 1, one_raw, length);
			returnResult[index++] = outData_7;
		}
		if (pos < last_raw_index - 2)
		{
			outData_9.pos = pos + width + 1;
			outData_9.value = getMaxValue(v_index, weight, width, pos + width + 1, one_raw, length);
			returnResult[index++] = outData_9;
		}
	}
	qsort(returnResult, index, sizeof(outMap), compare);

	cout << width << endl;
	outMap temp = returnResult[0];
	for (int i = 1; i < index; i++)
	{
		if (returnResult[i].value == temp.value)
		{
			continue;
		}
		cout << temp.value << " " << returnResult[i].pos - temp.pos << endl;
		temp = returnResult[i];
	}
	cout << temp.value << " " << weight[length]- temp.pos << endl;
	cout << "0 0" << endl;
}

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