UVA - 10037

别人写的题解:https://blog.csdn.net/qq_42505741/article/details/84852078
做这道题应该要有个感觉,是有个迭代或规律的。没思路时可以枚举一下,应该枚举到n=4就可以得出个思路了。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<stack>
#include<queue>
using namespace std;
const int maxn=1e3+6;
typedef long long ll;
ll t[maxn]; 

void count(int n)
{
	ll sum = 0;
	while(n >= 4)
	{
		sum += min(t[1] + 2*t[2] + t[n], 2*t[1] + t[n-1] + t[n]);
		n -= 2;
	}
	
	if(n == 1)
		sum += t[n];
	else if(n == 2)
		sum += t[n];
	else if(n == 3)
		sum += t[n-2]+t[n-1]+t[n];
	printf("%lld\n", sum);
}

void print(int n)
{
	while(n >= 4)
	{
		if(t[1] + 2*t[2] + t[n] < 2*t[1] + t[n-1] + t[n])
		{
			printf("%d %d\n", t[1], t[2]);
			printf("%d\n", t[1]);
			printf("%d %d\n", t[n-1], t[n]);
			printf("%d\n", t[2]);
		}
		else
		{
			printf("%d %d\n", t[1], t[n]);
			printf("%d\n", t[1]);
			printf("%d %d\n", t[1], t[n-1]);
			printf("%d\n", t[1]);
		}
		n -= 2;
	}
	if(n == 1)
		printf("%d\n", t[n]);
	else if(n == 2)
		printf("%d %d\n", t[n-1], t[n]);
	else if(n == 3)
	{
		printf("%d %d\n", t[n-2], t[n-1]);
		printf("%d\n", t[n-2]);
		printf("%d %d\n", t[n-2], t[n]);
	}
}

int main()
{
	int n, kase;
	scanf("%d", &kase);
	while(kase--)
	{
		scanf("%d", &n);
		for(int i=1; i<=n; ++i)
			scanf("%lld", &t[i]);
		sort(t+1, t+1+n);
		count(n);
		print(n);
		if(kase)	putchar('\n');
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值