题意:
给你n座山的高度,输出满足第一座山和最后一座山的高度差最小且难度最大的序列。
思路:
排序,先满足首尾,再将选择的那两座山的后面一部分接到头后头,把剩下的山从小到大再接在后面。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<map>
#include<queue>
#include<math.h>
using namespace std;
#define ll long long
#define mod 4933ll
ll h[200010];
ll ans[200010];
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
for(int i = 1; i <= n; i++)
{
cin>>h[i];
}
int flag = 0 ;
sort(h+1,h+1+n);
ll minn = 0x3f3f3f3f;
for(int i = 2; i <= n; i++)
{
if(minn > h[i]-h[i-1])
{
flag = i;
minn = h[i]-h[i-1];
}
}
ans[1] = h[flag-1],ans[n] = h[flag];
h[flag] = 0,h[flag-1] = 0;
for(int i = flag+1; i <= n; i++)
{
ans[i-flag+1] = h[i];
}
int cnt = 1;
for(int i = 1; i <= flag-2; i++)
{
ans[n-flag+1+i] = h[i];
}
for(int i = 1; i <= n; i++)
cout<<ans[i]<<" ";
cout<<endl;
}
}