1537C - Challenging Cliffs

题目:https://codeforces.com/problemset/problem/1537/C

题目大意:   有n(1<=n<=2*10^5)座山,每座山有一个高度hi,(1<=hi<=10^9),我们需要从左到右排列山的顺序,需要首先让第一座山和最后一座山的高度差最小,在此的基础上,若h_{i}<=h_{i+1} 则总难度加1,我们需要让总难度最大。输出任意一种满足条件的山的排列情况。

题解:只需要找出差距最小的h_{i},h_{i+1},将其以:h_{i},h_{i+2}....h_{n},h_{1},h_{2}......h_{i+1}排列就可以以最小的损失难度满足条件 

注:当n=2 时,可直接输出,需要特判

#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long int ll;

int a[200005];
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		int de = 1e9, d1;
		cin >> n;
		for (int i = 1; i <= n; i++)
		{
			cin >> a[i];
		}
		sort(a + 1, a + n + 1);
		if (n == 2)
		{
			cout << a[1] << " " << a[2] << endl;
		}
		else
		{
			for (int i = 1; i < n; i++)
			{
				int dex = a[i + 1] - a[i];
				if (dex < de)
				{
					de = dex;
					d1 = i;
				}
			}
			//cout << "d1=" << d1 << endl;
			for (int i = d1 + 1; i <= n; i++)
			{
				cout << a[i] << " ";
			}
			for (int i = 1; i <= d1; i++)
			{
				cout << a[i] << " ";
			}
			cout << endl;
		}
	}
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值