UVA Building designing
题目总结来说求一段序列,必须正负交替,且绝对值递增
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <vector> #include <algorithm> using namespace std; #define lowbit(x) (x&(-x)) #define max(x,y) (x>y?x:y) #define min(x,y) (x<y?x:y) #define mem(a) (memset(a,0,sizeof(a))) typedef long long ll; bool cmp(int a,int b) { return abs(a)<abs(b); } int main() { int t,n,a[500009]; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&a[i]); } sort(a,a+n,cmp); int ok=1,ans=1; if(a[0]<0) ok=1; else ok=0; for(int i=1;i<n;i++) { if(ok==0 && a[i]<0) ok=1,ans++; else if(ok==1 && a[i]>0) ok=0,ans++; } printf("%d\n",ans); } return 0; }