UVA11039 Building designing (贪心)

来源:互联网 发布:linux创建目录下的文件 编辑:程序博客网 时间:2024/06/09 23:39

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

题解:
可以拿出来随便摆的话,显然是贪心。
那么直接正负的交错搞就行了。

#include<bits/stdc++.h>using namespace std;const int maxn=5e5+5;int A[maxn];int B[maxn];int Ans[maxn];int Asize,Bsize;int main() {#ifdef tangge    freopen("11039.in","r",stdin);#endif // tangge    int n,val,T;    scanf("%d",&T);    while(T--) {        scanf("%d",&n);        Asize=Bsize=0;        for(int i=0; i<n; ++i) {            scanf("%d",&val);            if(val>0) {                A[Asize++]=val;            } else B[Bsize++]=val;        }        sort(A,A+Asize);        sort(B,B+Bsize,greater<int>() );        int ans=0,nowans=0;        int Al=0,Bl=0;        while(Al<=Asize&&Bl<=Bsize) {            if(nowans) {                while(Al<Asize&&A[Al++]<=Ans[nowans-1]) {}                if(Al<=Asize&&A[Al-1]>Ans[nowans-1])                    Ans[nowans++]=A[Al-1];                else break;            } else if(Asize>0)                Ans[nowans++]=A[Al],++Al;            while(Bl<Bsize&&abs(B[Bl++])<=Ans[nowans-1]) {}            if(Bl<=Bsize&&abs(B[Bl-1])>Ans[nowans-1])                Ans[nowans++]=abs(B[Bl-1]);            else break;        }        ans=max(ans,nowans);        Al=Bl=nowans=0;        while(Al<=Asize&&Bl<=Bsize) {            if(nowans) {                while(Bl<Bsize&&abs(B[Bl++])<=Ans[nowans-1]) {}                if(Bl<=Bsize&&abs(B[Bl-1])>Ans[nowans-1])                    Ans[nowans++]=abs(B[Bl-1]);                else break;            } else if(Bsize>0)                Ans[nowans++]=abs(B[Bl++]);            while(Al<Asize&&A[Al++]<=Ans[nowans-1]) {}            if(Al<=Asize&&A[Al-1]>Ans[nowans-1])                Ans[nowans++]=A[Al-1];            else break;        }        ans=max(ans,nowans);        printf("%d\n",ans);    }    return 0;}/*457 -2 6 9 -3811 -9 2 5 18 17 -15 410-1 2 -3 4 -5 6 -7 8 9 -1010-1 2 -3 4 -5 -6 -7 8 -9 -10*/
0 0
原创粉丝点击