110403 Bridge


#include <iostream>
#include <vector>

using namespace std;

template <typename T>
static void Swap(T* data, int index1, int index2)
{
	if (index1 == index2)
		return;

	T temp = *(data + index1);
	*(data + index1) = *(data + index2);
	*(data + index2) = temp;
}

template <typename T, typename GreaterThanFunc>
static int Partition(T* data, int left, int right, GreaterThanFunc greaterThanFunc)
{
	T& ref = data[right];
	int lastSmallerThanRefIndex = left - 1;
	for (int i = left; i < right; ++i)
	{
		if (greaterThanFunc(ref, *(data + i)))
		{
			++lastSmallerThanRefIndex;
			Swap(data, lastSmallerThanRefIndex, i);
		}
	}

	++lastSmallerThanRefIndex;
	Swap(data, lastSmallerThanRefIndex, right);

	return lastSmallerThanRefIndex;
}

template <typename T, typename GreaterThanFunc>
static void QuickSort(T* data, int left, int right, GreaterThanFunc greaterThanFunc)
{
	if (left >= right)
		return;

	if ((right - left) == 1)
	{
		if ((*(data + left)) > (*(data + right)))
			Swap(data, left, right);
		return;
	}

	int mid = Partition(data, left, right, greaterThanFunc);
	QuickSort(data, left, mid - 1, greaterThanFunc);
	QuickSort(data, mid + 1, right, greaterThanFunc);
}

template <typename T>
class Comparer
{
public:
	bool operator()(const T& a, const T& b)
	{
		return (a > b);
	}
};

struct CrossPair
{
public:
	CrossPair(int x, int y) : m_X(x), m_Y(y) {}
	CrossPair(int x) : m_X(x), m_Y(-1) {}

	void Display()
	{
		cout << m_X;
		if (m_Y > 0)
			cout << ' ' << m_Y;
		cout << endl;
	}

private:
	int m_X;
	int m_Y;
};

static int CrossBridge(int* people, int cnt, vector<CrossPair>& crossPairs)
{
	if (cnt == 0)
		return 0;

	if (cnt == 1)
	{
		crossPairs.push_back(CrossPair(people[0]));
		return people[0];
	}

	if (cnt == 2)
	{
		crossPairs.push_back(CrossPair(people[0], people[1]));
		return people[1];
	}

	if (cnt == 3)
	{
		crossPairs.push_back(CrossPair(people[0], people[2]));
		crossPairs.push_back(CrossPair(people[0]));
		crossPairs.push_back(CrossPair(people[0], people[1]));
		return people[0] + people[1] + people[2];
	}

	if (cnt == 3)
	{
		crossPairs.push_back(CrossPair(people[0], people[2]));
		crossPairs.push_back(CrossPair(people[0]));
		crossPairs.push_back(CrossPair(people[0], people[1]));
		return people[0] + people[1] + people[2];
	}

	if ((people[0] + people[cnt - 2]) > (2 * people[1]))
	{
		crossPairs.push_back(CrossPair(people[0], people[1]));
		crossPairs.push_back(CrossPair(people[0]));
		crossPairs.push_back(CrossPair(people[cnt - 2], people[cnt - 1]));
		crossPairs.push_back(CrossPair(people[1]));
		return people[1] * 2 + people[0] + people[cnt - 1] + CrossBridge(people, cnt - 2, crossPairs);
	}
	else
	{
		crossPairs.push_back(CrossPair(people[0], people[cnt - 1]));
		crossPairs.push_back(CrossPair(people[0]));
		crossPairs.push_back(CrossPair(people[0], people[cnt - 2]));
		crossPairs.push_back(CrossPair(people[0]));
		return people[0] * 2 + people[cnt - 2] + people[cnt - 1] + CrossBridge(people, cnt - 2, crossPairs);
	}
}

static void RunTestCase()
{
	int cnt;
	cin >> cnt;

	if (cnt <= 0)
	{
		cout << 0 << endl;
		return;
	}

	int* people = new int[cnt];

	for (int i = 0; i < cnt; ++i)
		cin >> people[i];

	QuickSort(people, 0, cnt - 1, Comparer<int>());

	vector<CrossPair> crossPairs;
	int time = CrossBridge(people, cnt, crossPairs);
	cout << time << endl;
	for (size_t i = 0; i < crossPairs.size(); ++i)
		crossPairs[i].Display();

	delete[] people;
}

static void Test()
{
	int testCnt;
	cin >> testCnt;

	for (int i = 0; i < testCnt; ++i)
	{
		RunTestCase();
		if (i < (testCnt - 1))
			cout << endl;
	}
}

int main(int argc, char* argv[])
{
	Test();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值