首页 > 代码库 > UVA - 11039 B - Building designing

UVA - 11039 B - Building designing

https://odzkskevi.qnssl.com/516b2b3ad824ada50248f23d20f53083?v=1502089438

 

 

 1 /*
 2 n个绝对值各不相同的非0整数,选出尽量多的数,排成一个序列,使得正负号交替且绝对值递增
 3 */
 4 #include <iostream>
 5 #include <string.h>
 6 #include <stdio.h>
 7 #include <algorithm>
 8 using namespace std;
 9 const int N=500005;
10 int t,n,num[N];
11  bool cmp(int a,int b){
12     return abs(a)<abs(b);  //升序排序
13 }
14  void init(){
15     scanf("%d",&n);
16     for(int i=0;i<n;i++)
17     scanf("%d",&num[i]);
18     sort(num,num+n,cmp);     //sort函数应用
19  }
20  int solve(){
21     int ans=1,flag=0;
22     if(num[0]<0) flag=0;
23     else flag=1;
24     for(int i=0;i<n;i++){
25         if(flag==0&&num[i]>0){
26             flag=1;ans++;
27         }
28         else if(flag==1&&num[i]<0){
29             flag=0;ans++;
30         }
31     }
32     return ans;
33  }
34 int main(){
35     scanf("%d",&t);
36     while(t--){
37         init();
38         printf("%d\n",solve());
39     }
40     return 0;
41 }

 

UVA - 11039 B - Building designing