其他分享
首页 > 其他分享> > 1537C - Challenging Cliffs

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;
		}
	}
}

 

 

 

标签:满足条件,Cliffs,Challenging,int,cin,long,1537C,include,难度
来源: https://blog.csdn.net/Wzwan_huai/article/details/118860067