UVA_11039_BuildingDesigning

来源:互联网 发布:stm32f 输入端口写法 编辑:程序博客网 时间:2024/06/05 15:58

11039 - Building designing

Time limit: 3.000 seconds

An architect wants to design a very high building. The building will consist of some 
oors, and each


oor has a certain size. The size of a 
oor must be greater than the size of the 
oor 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 
oor a colour, and in such a way that the colours of two consecutive


oors are different.
To design the building the architect has n available 
oors, with their associated sizes and colours.
All the available 
oors are of different sizes. The architect wants to design the highest possible building
with these restrictions, using the available 
oors.
Input
The input le consists of a rst line with the number p of cases to solve. The rst line of each case
contains the number of available 
oors. Then, the size and colour of each 
oor appear in one line.
Each 
oor is represented with an integer between -999999 and 999999. There is no 
oor with size 0.
Negative numbers represent red 
oors and positive numbers blue 
oors. The size of the 
oor is the
absolute value of the number. There are not two 
oors with the same size. The maximum number of


oors for a problem is 500000.
Output
For each case the output will consist of a line with the number of 
oors 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 <iostream>#include <stdio.h>#include <algorithm>using namespace std;const int M=500005;int z[M],f[M];int main(){    int t,n,p1,p2,num;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        p1=0;p2=0;        for(int i=1;i<=n;i++)        {            scanf("%d",&num);            if(num>0)                z[p1++]=num;            else                f[p2++]=-num;        }        sort(z,z+p1);        sort(f,f+p2);        int pp1=0,pp2=0,co1=1,co2=1;        while(pp1<p1&&pp2<p2)        {            while(pp2<p2&&f[pp2]<=z[pp1])                pp2++;            //cout<<pp2<<" ";            if(pp2!=p2)                co1++;            else                break;            while(pp1<p1&&z[pp1]<=f[pp2])                pp1++;            //cout<<pp1<<" ";            if(pp1!=p1)                co1++;            else                break;        }        //cout<<endl;        pp1=0,pp2=0;        while(pp1<p1&&pp2<p2)        {            while(pp1<p1&&z[pp1]<=f[pp2])                pp1++;            //cout<<pp1<<" ";            if(pp1!=p1)                co2++;            else                break;            while(pp2<p2&&f[pp2]<=z[pp1])                pp2++;            //cout<<pp2<<" ";            if(pp2!=p2)                co2++;            else                break;        }        //cout<<endl;        printf("%d\n",max(co1,co2));    }    return 0;}


0 0
原创粉丝点击