uva 11039 Building designing

https://vjudge.net/problem/UVA-11039

题意:

给出n个不相同的非0整数,要求从其中选出一些数组成一个数列,这个数列绝对值递增但是正负相间,输出最长序列的长度。

思路:

先按照绝对值大小排序,然后按照正负相间选就行了。

代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 int a[500000+5];
 7 
 8 int mabs(int b)
 9 {
10     return b >= 0 ? b : -b;
11 }
12 
13 bool cmp(int b,int c)
14 {
15     return mabs(b) < mabs(c);
16 }
17 
18 int main()
19 {
20     int t;
21 
22     scanf("%d",&t);
23 
24     while (t--)
25     {
26         int n;
27 
28         scanf("%d",&n);
29 
30         for (int i = 0;i < n;i++)
31             scanf("%d",a + i);
32 
33         sort(a,a+n,cmp);
34 
35         int cnt = 1;
36         int pre = a[0];
37 
38         for (int i = 1;i < n;i++)
39         {
40             if (pre < 0 && a[i] > 0)
41             {
42                 pre = a[i];
43                 cnt++;
44             }
45 
46             if (pre > 0 && a[i] < 0)
47             {
48                 pre = a[i];
49                 cnt++;
50             }
51         }
52 
53         printf("%d\n",cnt);
54     }
55 
56     return 0;
57 }

 

转载于:https://www.cnblogs.com/kickit/p/7920399.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值