首页 > 代码库 > 11039 - Building designing

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 

 

 257-269-3811-9251817-154

Sample Output 

 

 25

 

 

 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         else51         {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

 

11039 - Building designing