UVA11039 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

题解:排序+贪心。。。按绝对值从小到大排序,然后红蓝间隔的选取瓷砖。。。好水。。。

View Code
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #define MAXSN 500000
 4 long f[MAXSN+5],a[MAXSN+5],v[MAXSN+5];
 5 void qsortt(long l,long r)
 6 {
 7     long i,j,mid,temp;
 8     i=l;
 9     j=r;
10     mid=f[(l+r)>>1];
11     while(i<=j)
12     {
13         while(f[i]<mid) i++;
14         while(f[j]>mid) j--;
15         if(i<=j)
16         {
17             temp=f[i];
18             f[i]=f[j];
19             f[j]=temp;
20             temp=v[i];
21             v[i]=v[j];
22             v[j]=temp;
23             i++;
24             j--;
25         }
26     }
27     if(i<r) qsortt(i,r);
28     if(j>l) qsortt(l,j);
29 }
30 int main(void)
31 {
32     long i,m,n,ans,flag,t;
33     scanf("%ld",&n);
34     while(n--)
35     {
36         scanf("%ld",&m);
37         for(i=0; i<m; i++)
38         {
39             scanf("%ld",&v[i]);
40             f[i]=abs(v[i]);
41         }
42 
43         qsortt(0,m-1);
44         for(i=0; i<m; i++)
45             if(v[i]>0) a[i]=1;
46             else a[i]=0;
47         i=1;
48         ans=1;
49         flag=1;
50         t=a[0];
51         while(i<m)
52         {
53             while(((t&&a[i])||(!t&&!a[i]))&&i<m) i++;
54             if(i<m)
55             {
56                 ans++;
57                 t=a[i];
58             }
59             i++;
60 
61         }
62         printf("%ld\n",ans);
63     }
64     return 0;
65 }
 
原文地址:https://www.cnblogs.com/zjbztianya/p/2945350.html