zoukankan      html  css  js  c++  java
  • 11039

      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
    
     1 #include <iostream>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <cmath>
     5 using namespace std;
     6 
     7 const int MAXN = 500100;
     8 int t, postive[MAXN], negative[MAXN];
     9 
    10 bool cmp2(int a, int b)
    11 {
    12     return a < b;
    13 }
    14 
    15 int main()
    16 {
    17     cin >> t;
    18     while(t--)
    19     {
    20         memset(postive, 0, sizeof(MAXN));
    21         memset(negative, 0, sizeof(MAXN));
    22         int j = 0, k = 0;
    23         int n, res = 0, tag = 0;
    24         cin >> n;
    25         for(int i = 1; i <= n; ++i)
    26         {
    27             int cur;
    28             cin >> cur;
    29             if(cur > 0)
    30             {
    31                 postive[j++] = cur;
    32             }
    33             else if(cur < 0)
    34             {
    35                 negative[k++] = -cur;
    36             }
    37         }
    38 
    39         sort(postive, postive+j, cmp2);
    40         sort(negative, negative+k, cmp2);
    41         int l = 0,m = 0;
    42         int Max;
    43         if(negative[0] < postive[0])
    44         {
    45             tag = 1;
    46             Max = negative[0];
    47             res++;
    48             m++;
    49         }
    50         else
    51         {
    52             tag = 0;
    53             Max = postive[0];
    54             res++;
    55             l++;
    56         }
    57 
    58         for(int i = 1; i <= n; ++i)
    59         {
    60             if(tag == 0)
    61             {
    62                 while((m < k) && (tag == 0))
    63                 {
    64                     if(negative[m] > Max)
    65                     {
    66                         res++;
    67                         Max = negative[m];
    68                         tag = 1;
    69                     }
    70                     m++;
    71                 }
    72             }
    73             else if(tag == 1)
    74             {
    75                 while((l < j) && (tag == 1))
    76                 {
    77                     if(postive[l] > Max)
    78                     {
    79                         res++;
    80                         Max = postive[l];
    81                         tag = 0;
    82                     }
    83                     l++;
    84                 }
    85             }
    86 
    87         }
    88 
    89         cout << res << endl;
    90     }
    91     return 0;
    92 }
    View Code
  • 相关阅读:
    JSON基本操作
    常用的windowd属性和对象
    手动建立数据库连接的BaseDAO
    注意1:图像插值理论的理解
    Python的Scikit-learn如何选择合适的机器学习算法?
    spark-sklearn(spark扩展scikitlearn)
    Tips-Windows 10【多桌面视窗】操作
    jupyter notebook快捷键速查手册
    使用IntelliJ IDEA进行Python远程调试的需求(未完)
    Bazel构建工具的安装
  • 原文地址:https://www.cnblogs.com/ya-cpp/p/3976877.html
Copyright © 2011-2022 走看看