本质上是DP,但是俩变量就搞定了。
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 int p[500001]; 6 bool cmp(int a,int b) 7 { 8 if(a < 0) 9 a = -a; 10 if(b < 0) 11 b = -b; 12 return a < b; 13 } 14 int main() 15 { 16 int t,n,p1,p2,i; 17 scanf("%d",&t); 18 while(t--) 19 { 20 scanf("%d",&n); 21 for(i = 0;i < n;i ++) 22 { 23 scanf("%d",&p[i]); 24 } 25 sort(p,p+n,cmp); 26 p1 = p2 = 0; 27 for(i = 0;i < n;i ++) 28 { 29 if(p[i] > 0) 30 p1 = p2 + 1; 31 else 32 p2 = p1 + 1; 33 } 34 printf("%d ",max(p1,p2)); 35 } 36 }