问题描述
- 给你一组数,要你求满足条件的最长子串长度。
- 条件是对于这个子串任意的(i,j)(i,j)(i,j)对,都有∣ai−aj∣>=MAX{|a_i - a_j|} >= MAX∣ai−aj∣>=MAX,MAXMAXMAX是指留下的子串中的最大值。
思路分析
- 实际上,很容易想到,在一组数中,每一个负数和000,我们都可以选上。
- 那么剩下的就是非负数了,那么我们在这个地方就要小心,因为得出来的子串的∣ai−aj∣|a_i - a_j|∣ai−aj∣值不同,也没有大小顺序可言,所以要用个变量储存一下∣ai−aj∣|a_i - a_j|∣ai−aj∣最小值,然后再对后面的正数进行添加(很容易想到最多加一个),所以就需要判断第一个正数是否符合即可。
代码如下
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int a[maxn];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
int x = 1e9 + 10;
int cnt = 0;
memset(a, 0, sizeof(a));
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
sort(a + 1, a + 1 + n);
int i;
int pos = 1;
for (i = 1; i <= n; i++)
{
if (i >= 2)
{
x = min(x, a[i] - a[i - 1]);
}
if (a[i] <= 0)
cnt++;
else
break;
}
if (i <= n && a[i] <= x)
{
cnt++;
}
cout << cnt << endl;
}
return 0;
}