UVA - 11039 Building designing

来源:互联网 发布:淘宝店铺商品详情模板 编辑:程序博客网 时间:2024/05/17 05:08
Building designing
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

Submit Status

Description

Download as PDF


  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





分析:

刘汝佳大白书里的题。排序+模拟,简单题。模拟就是用代码描述题目的过程。注意计算过程数组不要越界。

ac代码:

#include <iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=500000+10;
int a[maxn],b[maxn];


int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,an=0,bn=0,x;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&x);
            if(x>0)
            a[an++]=x;
            else
            b[bn++]=-x;
        }
        //if(!an||!bn) printf("1\n");//包含在楼下else语句中,可省
        //else
        //{
            sort(a,a+an);
            sort(b,b+bn);
            bool flag;//flag表示从a还是b往后枚举,flag=1从a往后,flag=0从b往后
            int ans=1;
            if(a[0]<b[0]) flag=1;
            else flag=0;
            int i=0,j=0;
            while(i<an&&j<bn)
            {
                if(flag)
                {
                    flag=0;
                    while(a[i]<b[j]&&i<an) i++;//i<an,防止数组越界
                    ans++;
                }
                else
                {
                    flag=1;
                    while(b[j]<a[i]&&j<bn) j++;
                    ans++;
                }
            }
            printf("%d\n",ans);
            //}
        }
    return 0;
}

0 0
原创粉丝点击