• UVA 11039.Building designing


    Building designing

    Time limit: 3.000 seconds

    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>
    const int N=5*1e5+10;
    using namespace std;
    int a[N];
    bool cmp(int a,int b){
        return abs(a)<abs(b);
    }
    int main(){
        int t,n,flag,num;
        while(~scanf("%d",&t)){
         while(t--){
            scanf("%d",&n);
            flag=0;num=0;
            for(int i=0;i<n;i++)
                scanf("%d",&a[i]);
                sort(a,a+n,cmp);
                if(a[0]>0)flag=1;                     //这里一开始写错了,写成flag=1了。。。
                else flag=2;
                num++;
            for(int i=1;i<n;i++){
                if(flag==1){
                    if(a[i]<0){flag=2;num++;}
                }
                else if(flag==2){
                    if(a[i]>0){flag=1;num++;}
                }
            }
            printf("%d
    ",num);
        }
        }
        return 0;
    }
  • 相关阅读:
    无线网络的切换bat
    远程桌面远程服务器拷贝失灵 解决方法
    不能将 Null 值赋给类型为 (不可为 null 的值类型)的成员。解决方法
    应该改变面向对象的说法
    Windows 2012 装 Remote Desktop Organizer 无法连接到其他远程服务器
    JavaScript 运行时错误: 无法获取未定义或 null 一种解决方案
    ssd硬盘u盘装win7扩展文件时0x80070570错误
    swfit-小知识Demo
    ios-简单算法
    swfit-扩展语法
  • 原文地址:https://www.cnblogs.com/ZERO-/p/7182909.html
Copyright © 2020-2023  润新知