nth_element 求助!
  • 板块学术版
  • 楼主Asher12
  • 当前回复2
  • 已保存回复2
  • 发布时间2023/11/10 21:07
  • 上次更新2023/11/10 22:21:46
查看原帖
nth_element 求助!
836643
Asher12楼主2023/11/10 21:07

这是一个求值问题,具体如下:

1.对于一个降序排序序列 aa ,对其相邻两个数 aia_{i} 与 ai+1a_{i+1},求差 eie_{i} (小数减大数)并输出最小值。

2.对于一个非降序排序数列 aa ,输出其序列最大值与最小值的差。

代码如下:

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
long long a[200010];
long long b[200010];
bool cmp(long long x,long long y)
{
	return abs(x)<abs(y);
}
int main()
{
//	freopen("money.in","r",stdin);
//	freopen("money.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin>>n;
	bool x=true;
	for (int i=0;i<n;i++)
	{
		cin>>a[i];
		if (i>=1&&x==true)
		{
			x=(a[i-1]>a[i]);
			b[i]=a[i]-a[i-1];
		}
	}
	if (x==true)
	{
		nth_element(b,b+2,b+n);
		cout<<b[1]<<'\n';
		return 0;
	}
	nth_element(a,a+1,a+n);
	long long xx=a[0];
	nth_element(a,a+n,a+n);
	cout<<a[n-1]-xx<<'\n';
	return 0;
}

该程序不能输出正确结果,想请教一下,哪里有问题?

2023/11/10 21:07
加载中...