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=*1e5+;
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=;num=;
for(int i=;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n,cmp);
if(a[]>)flag=; //这里一开始写错了,写成flag=1了。。。
else flag=;
num++;
for(int i=;i<n;i++){
if(flag==){
if(a[i]<){flag=;num++;}
}
else if(flag==){
if(a[i]>){flag=;num++;}
}
}
printf("%d\n",num);
}
}
return ;
}

UVA11039-Building designing的更多相关文章

  1. 11039 - Building designing

      Building designing  An architect wants to design a very high building. The building will consist o ...

  2. 贪心水题。UVA 11636 Hello World,LA 3602 DNA Consensus String,UVA 10970 Big Chocolate,UVA 10340 All in All,UVA 11039 Building Designing

    UVA 11636 Hello World 二的幂答案就是二进制长度减1,不是二的幂答案就是是二进制长度. #include<cstdio> int main() { ; ){ ; ) r ...

  3. UVA 11039 - Building designing(DP)

    题目链接 本质上是DP,但是俩变量就搞定了. #include <cstdio> #include <cstring> #include <algorithm> u ...

  4. UVa 11039 - Building designing

    题目大意:n个绝对值各不相同的非0整数,选出尽量多的数,排成一个序列,使得正负号交替且绝对值递增. 分析:按照绝对值大小排一次序,然后扫描一次,顺便做个标记即可. #include<cstdio ...

  5. UVa 11039 (排序+贪心) Building designing

    白书上的例题比较难,认真理解样例代码有助于提高自己 后面的练习题相对简单,独立思考解决问题,增强信心 题意:n个绝对值各不相同的非0整数,选出尽量多的数排成序列,使得该序列正负交错且绝对值递增. 解法 ...

  6. Building designing UVA - 11039

    先取正的和负的绝对值较小者为开头 .然后交替从正负数中取绝对值最小但比上一个大的. 证明: 1.开头选正负数中绝对值较小的:否则能再多放1个. 2.交替选的时候选最小的符合条件的:如果大的符合,换小的 ...

  7. UVa 11039 - Building designing 贪心,水题 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  8. UVa 11039 Building designing (贪心+排序+模拟)

    题意:给定n个非0绝对值不相同的数,让他们排成一列,符号交替但绝对值递增,求最长的序列长度. 析:我个去简单啊,也就是个水题.首先先把他们的绝对值按递增的顺序排序,然后呢,挨着扫一遍,只有符号不同才计 ...

  9. UVA 11039 Building designing 贪心

    题目链接:UVA - 11039 题意描述:建筑师设计房子有两条要求:第一,每一层楼的大小一定比此层楼以上的房子尺寸要大:第二,用蓝色和红色为建筑染色,每相邻的两层楼不能染同一种颜色.现在给出楼层数量 ...

  10. UVA Building designing

    题目总结来说求一段序列,必须正负交替,且绝对值递增 #include <iostream> #include <cstdio> #include <cstring> ...

随机推荐

  1. 数据库入门(以MySQL为例)

    一.数据库中的概念 1.数据库是用户存放数据.访问数据.操作数据的存储仓库,用户的各种数据被有组织地存放在数据库中.可以随时被有权限的用户查询.统计.添加.删除.和修改.可以说,数据库是长期存储在计算 ...

  2. lodash源码分析之Hash缓存

    在那小小的梦的暖阁,我为你收藏起整个季节的烟雨. --洛夫<灵河> 本文为读 lodash 源码的第四篇,后续文章会更新到这个仓库中,欢迎 star:pocket-lodash gitbo ...

  3. MQ NameServer模块划分

    上图是之前讨论确定的系统架构(后续内容会按照这个架构来叙述),其中: NameServer做Broker的服务发现,即客户端可以通过NameServer拿到Broker的信息 Broker汇报数据到N ...

  4. PXE+Kickstart 全自动安装部署CentOS7.4

    一.简介 1.什么是PXE PXE(preboot execute environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作站通过 ...

  5. lesson - 8 课程笔记 tar / gzip /bzip2 / xz /

    作用:为linux的文件和目录创建档案,也可以在档案中改变文件,或者向档案中加入新的文件即用来压缩和解压文件.tar本身不具有压缩功能.他是调用压缩功能实现的  语法:tar[必要参数][选择参数][ ...

  6. RedisPool操作Redis,工具类实例

    redis.properties 配置文件内容 redis.pool.maxActive=100redis.pool.maxIdle=20redis.pool.maxWait=3000redis.po ...

  7. JDK8中JVM对类的初始化探讨

    在<深入理解Java虚拟机>(第二版,周志明著)中,作者介绍了JVM必须初始化类(或接口)的五种情况,但是是针对JDK7而言的. 那么,在JDK8中,这几种情况有没有变化呢?(我猜测应该会 ...

  8. .net 委托的使用方法以及使用委托的好处

    使用方法: //无返回值无参数委托的定义方法 public delegate void NoReturnPara(); 给委托赋值的几种方式 //实例化委托,并传入方法 NoReturbNoPara ...

  9. Linux发行版 CentOS6.5下删除分区操作

    本文地址http://comexchan.cnblogs.com/,作者Comex Chan,尊重知识产权,转载请注明出处,谢谢!   有时候,发现分区分错了.需要删除分区,只需按照分区的步骤逆向操作 ...

  10. JAVA个人理解

    为了找到别人写的好文章,先分享下自己的知识,找找感觉路线. 学java前接触的c,后来转向java.第一个照面理解的就是面向对象,没想到让我想了好多年.当时有个负责任的老师说面向对象这个词具体释义众说 ...