zoukankan      html  css  js  c++  java
  • uva11039

      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

    题目大意:给你n个绝对值不相等的数,要求你选择数字排成序列,使得正负交替且绝对值递增。输出最长的序列长度。
    分析:水题,按绝对值排序后,正负交替输出即可。
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #define maxlen 500010
     6 using namespace std;
     7 int num[maxlen];
     8 bool cmp(int a,int b)
     9 {
    10     return abs(a)<abs(b);
    11 }
    12 int main ()
    13 {
    14     int n,t;
    15     scanf("%d",&t);
    16     while(t--)
    17     {
    18         scanf("%d",&n);
    19         for(int i=0;i<n;++i)
    20             scanf("%d",&num[i]);
    21         sort(num,num+n,cmp);
    22         int ans=1;
    23         int flag=num[0]>0;
    24         for(int i=1;i<n;++i)
    25         {
    26             if(num[i]<0&&flag)
    27             {
    28                 ans++;
    29                 flag=0;
    30             }
    31             else if(num[i]>0&&!flag)
    32             {
    33                 ans++;
    34                 flag=1;
    35             }
    36         }
    37         printf("%d
    ",ans);
    38     }
    39 
    40 }
    View Code
  • 相关阅读:
    洛谷 P1706 全排列
    n皇后问题
    跳马
    [HDOJ4612]Warm up(双连通分量,缩点,树直径)
    [POJ3177]Redundant Paths(双连通图,割边,桥,重边)
    [POJ3352]Road Construction(缩点,割边,桥,环)
    [POJ3694]Network(LCA, 割边, 桥)
    [UVA796]Critical Links(割边, 桥)
    [UVA315]Network(tarjan, 求割点)
    [HDOJ2586]How far away?(最近公共祖先, 离线tarjan, 并查集)
  • 原文地址:https://www.cnblogs.com/shuzy/p/3188220.html
Copyright © 2011-2022 走看看