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块瓷砖,求出可以建造的最大的高度是多少。需要注意的是,当输入的数是正数时,代表这块地板的颜色是蓝色的,当输入的数是负数时,代表这块地板的颜色是红色的。题目中也要求不同颜色的地板才能镶嵌盖,相同颜色的地板是不能放在一起的,并且下面的地板的大小必须大于上面的地板的大小。注意:每进行一次测试时都需要将数组清零。
程序代码:
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; const long long N=1000000; long long a[N],b[N],c[N],d[N]; int main() { int t,i,j,k,f,g; long long maximun,n; char floag; scanf("%d",&t); while(t--) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); memset(d,0,sizeof(d)); f=0,g=0; floag=‘x‘; scanf("%lld",&n); for(i=0;i<n;i++) scanf("%lld",&c[i]); for(i=0,j=0,k=0;i<n;i++) { if(c[i]>0) {a[j]=c[i];j++;f=1;} else {b[k]=c[i]*(-1);k++;g=1;} } if(f==1&&g==1)maximun=2; else maximun=1; sort(a,a+j-1); sort(b,b+k-1); if(maximun==2) { for(i=0,j=0,k=0;i<n;i++) { if(a[j]<b[k]){d[i]=a[j];j++;if(floag==‘b‘)maximun++;floag=‘a‘;} else{d[i]=b[k];k++;if(floag==‘a‘)maximun++;floag=‘b‘;} } } printf("%lld\n",maximun); } return 0; }