B. Sifid and Strange Subsequences
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
A sequence (b1,b2,…,bk)(b1,b2,…,bk) is called strange, if the absolute difference between any pair of its elements is greater than or equal to the maximum element in the sequence. Formally speaking, it's strange if for every pair (i,j)(i,j) with 1≤i<j≤k1≤i<j≤k, we have |ai−aj|≥MAX|ai−aj|≥MAX, where MAXMAX is the largest element of the sequence. In particular, any sequence of length at most 11 is strange.
For example, the sequences (−2021,−1,−1,−1)(−2021,−1,−1,−1) and (−1,0,1)(−1,0,1) are strange, but (3,0,1)(3,0,1) is not, because |0−1|<3|0−1|<3.
Sifid has an array aa of nn integers. Sifid likes everything big, so among all the strange subsequences of aa, he wants to find the length of the longest one. Can you help him?
A sequence cc is a subsequence of an array dd if cc can be obtained from dd by deletion of several (possibly, zero or all) elements.
Input
The first line contains an integer tt (1≤t≤104)(1≤t≤104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains an integer nn (1≤n≤105)(1≤n≤105) — the length of the array aa.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109)(−109≤ai≤109) — the elements of the array aa.
It is guaranteed that the sum of nn over all test cases doesn't exceed 105105.
Output
For each test case output a single integer — the length of the longest strange subsequence of aa.
Example
input
Copy
6 4 -1 -2 0 0 7 -3 4 -2 0 -4 6 1 5 0 5 -3 2 -5 3 2 3 1 4 -3 0 2 0 6 -3 -2 -1 1 1 1
output
Copy
4 5 4 1 3 4
Note
In the first test case, one of the longest strange subsequences is (a1,a2,a3,a4)
In the second test case, one of the longest strange subsequences is (a1,a3,a4,a5,a7)
In the third test case, one of the longest strange subsequences is (a1,a3,a4,a5)
In the fourth test case, one of the longest strange subsequences is (a2)
In the fifth test case, one of the longest strange subsequences is (a1,a2,a4)
*******************************************************************************************************************************************************************************************************************
题目大意:给你一个n个元素的数组,让你找出这个数组的子数组中最长的奇怪数组的长度,奇怪数组指的是数组中任意的|ai−aj|≥MAX,1≤i<j≤k,MAX是数组中元素的最大值,i和j是数组的下标
解题思路:我们很容易可以证明出这个奇怪数组中最多只能有一个正数,我们首先看负数和零,两个负数的差的绝对值一定是大于等于0的,而这些数最大的就是0,所以只要是负数或者正数我们都要,再看正数部分,如果在负数和零组成的序列中加一个正数,那么他肯定满足奇怪数组的定义,那么如果我加两个或者好多个正数呢,我们假设a>b,&& a>0 && b>0,那么|a-b|一定小于a,所以可知这是存在两个及以上的正数是不成立的
我们做的时候只需要将负数和0放在一个数组中,将正数放在一个数组中,我们将两个数组排序,然后枚举负数和零数组间二者绝对值差的最小值,如果这个最小值大于等于正数的最小值,则答案加1,否则不加
下面附上ac代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define dbg(a) cout<<#a<<" : "<<a<<endl;
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL);
#define PAUSE system("pause")
#define sd(a) scanf("%d",&a)
#define sll(a) scanf("%lld",&a)
#define sdd(a,b) scanf("%d%d",&a,&b)
#define sddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sf(a) scanf("%lf",&a)
#define sff(a,b) scanf("%lf%lf",&a,&b)
#define sfff(a,b,c) scanf("%lf%lf%lf",&a,&b,&c)
using namespace std;
typedef long long ll;
typedef pair<int ,int> pr;
const int maxn=205;
const ll INF=1e9+5;
int n,m,t;
int sum;
int a[100005];
int b[100005];
int main()
{
sd(t);
while(t--)
{
sd(n);
int ans=0;
int ida=1;
int idb=1;
int flag=0;
for(int i=1;i<=n;i++)
{
sd(m);
if(m<=0){
a[ida++]=m;
ans++;
}
else{
flag=1;
b[idb++]=m;
}
}
sort(a+1,a+ida);
sort(b+1,b+idb);
int minn=INF;
for(int i=2;i<ida;i++)
{
minn=min(minn,abs(a[i]-a[i-1]));
}
if(minn>=b[1]&&flag==1){
ans++;
}
printf("%d\n",ans);
}
return 0;
}