UVA 11039 - Building designing

Building designing 

An architect wants to design a very high building. The building will consist of some floors, and each floor has a certain size. The size of a floor must be greater than the size of the floor immediately above it. In addition, the designer (who is a fan of a famous Spanish football team) wants to paint the building in blue and red, each floor a colour, and in such a way that the colours of two consecutive floors are different.

To design the building the architect has n available floors, with their associated sizes and colours. All the available floors are of different sizes. The architect wants to design the highest possible building with these restrictions, using the available floors.

 

Input 

The input file consists of a first line with the number p of cases to solve. The first line of each case contains the number of available floors. Then, the size and colour of each floor appear in one line. Each floor is represented with an integer between -999999 and 999999. There is no floor with size 0. Negative numbers represent red floors and positive numbers blue floors. The size of the floor is the absolute value of the number. There are not two floors with the same size. The maximum number of floors for a problem is 500000.

 

Output 

For each case the output will consist of a line with the number of floors of the highest building with the mentioned conditions. 

 

Sample Input 

 

 
2
5
7
-2
6
9
-3
8
11
-9
2
5
18
17
-15
4

 

Sample Output 

 

 
2
5

 Time limit: 3.000 seconds

 

题目大意:给n个绝对值不相等的数,选择尽量多的数排成正负交替且绝对值递增的序列。

把正负数分开,按绝对值从小到大排序,以绝对值最小的一个数开始,依次交替从正负数里面选择最小的且大于上一个数的数,直到不能选择。

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <vector>
 6 #include <map>
 7 #include <set>
 8 
 9 using namespace std;
10 
11 #ifdef WIN
12 typedef __int64 LL;
13 #define form "%I64d\n"
14 #else
15 typedef long long LL;
16 #define form "%lld\n"
17 #endif
18 
19 #define SI(a) scanf("%d", &(a))
20 #define S64I scanf(form, &a)
21 #define SS(a) scanf("%s", (a))
22 #define SC(a) scanf("%c", &(a))
23 #define PI(a) printf("%d\n", a)
24 #define P64I(a) printf(form, a)
25 #define Max(a, b) (a > b ? a : b)
26 #define Min(a, b) (a < b ? a : b)
27 #define MSET(a, b) (memset(a, b, sizeof(a)))
28 #define Mid(L, R) (L + (R - L)/2)
29 #define Abs(a) (a > 0 ? a : -a)
30 #define REP(i, n) for(int i=0; i < (n); i++) 
31 #define FOR(i, a, n) for(int i=(a); i <= (n); i++)
32 
33 
34 const int maxn = 500000 + 50;
35 int A[maxn], B[maxn];
36 
37 int cmp(int a, int b) {
38     return Abs(a) < Abs(b);
39 }
40 
41 
42 int main() {
43     int T;
44 
45     SI(T);
46     while(T--) {
47         int n, cnt1 = 0, cnt2 = 0;
48 
49         SI(n);
50         REP(i, n) {
51             int t;
52             SI(t);
53             if(t > 0) A[cnt1++] = t;
54             else B[cnt2++] = t;
55         }
56         sort(A, A+cnt1);
57         sort(B, B+cnt2, cmp);
58         int ans = 1;
59         int i = 0, j = 0;
60         int t = A[0] < Abs(B[0]) ? 1 : 0;
61         while(1) {
62             if(!t) {
63                 while(i < cnt1 && A[i] <= Abs(B[j])) i++;
64                 if(i == cnt1) break;
65                 ans++;
66                 t = 1;
67             } else {
68                 while(j < cnt2 && Abs(B[j]) <= A[i]) j++;
69                 if(j == cnt2) break;
70                 ans++;
71                 t = 0;
72             }
73         }
74         PI(ans);
75     }
76 
77     return 0;
78 }

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值