题目输入 -999999 - 999999
将负数变为正数,并且在num中标记为负数(true)
从后往前取,一正一负(一负一正)
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> using namespace std; #define maxn 1000000 int arr[maxn]; bool num[maxn]; int main() { int T,N,a; scanf("%d",&T); while(T--) { memset(num,false,sizeof(num)); scanf("%d",&N); for(int i = 0; i < N; i++) { scanf("%d",&arr[i]); if(arr[i]<0) { num[-arr[i]] = true; arr[i] = -arr[i]; } } sort(arr,arr+N); int ans = 1; bool flag = num[arr[N-1]]; for(int i = N-2; i >= 0; i--) { if(num[arr[i]]!=flag) { ans++; flag = num[arr[i]]; } } printf("%d ",ans); } return 0; }