UVA11039-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

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

题意:建一栋楼,负数代表一种颜色,正数代表另一种颜色,要正负号交替且绝对值递增。

两种解法:

第一种就是简单排下序,然后贪心的思想不断找绝对值最小的就可以了,注意的就是先取正值和先取负值的情况都要考虑

还有一种就是直接对绝对值进行排序操作,然后标记正负号(貌似更简练,感谢欧尼酱的点播)

解法1AC代码:

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')
f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
inline void write(int x)
{
if(x<)
{
putchar('-');
x=-x;
}
if(x>)
{
write(x/);
}
putchar(x%+'');
}
const int N=;
int a[N],b[N];
int main()
{
int t,n,p1,p2,num;
t=read();
while(t--)
{
n=read();
p1=;
p2=;
for(int i=;i<=n;i++)
{
scanf("%d",&num);
if(num>)
a[p1++]=num;
else
b[p2++]=-num;
}
sort(a,a+p1);
sort(b,b+p2);
int m1=,m2=,n1=,n2=;
while(m1<p1&&m2<p2)
{
while(m2<p2&&b[m2]<=a[m1])
m2++;
if(m2!=p2)
n1++;
else
break;
while(m1<p1&&a[m1]<=b[m2])
m1++;
if(m1!=p1)
n1++;
else
break;
}
m1=,m2=;
while(m1<p1&&m2<p2)
{
while(m1<p1&&a[m1]<=b[m2])
m1++;
if(m1!=p1)
n2++;
else
break;
while(m2<p2&&b[m2]<=a[m1])
m2++;
if(m2!=p2)
n2++;
else
break;
}
printf("%d\n",max(n1,n2));
}
return ;
}

解法2AC代码:(欧尼酱的代码,参考一下)

 #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=;
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 ;
}

UVA 11039-Building designing【贪心+绝对值排序】的更多相关文章

  1. UVA 11039 Building designing 贪心

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

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

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

  3. 贪心水题。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 ...

  4. UVA 11039 - Building designing 水题哇~

    水题一题,按绝对值排序后扫描一片数组(判断是否异号,我是直接相乘注意中间值越界)即可. 感觉是让我练习sort自定义比较函数的. #include<cstdio> #include< ...

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

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

  6. UVa 11039 - Building designing

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

  7. UVA 11039 - Building designing(DP)

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

  8. 11039 - Building designing

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

  9. HDOJ2020绝对值排序

    绝对值排序 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

随机推荐

  1. C:函数:功能:实现字符数组中所有字母的倒序存放并输出

    前两天小测碰到一道题,建立一个函数,功能:实现字符数组中所有字母的倒序存放并输出,一开始觉得简单跟数字数组差不多,运行一下发现很多格式错误,这些是不必要的错误,现在就来说下,先说一下代码思路:定义一个 ...

  2. 1.Nginx服务应用

    Nginx服务应用 Nginx的优点和作用 Nginx是一款高性能的HTTP和反向代理的服务器软件,还是一个IMAP/POP3/SMTP(邮件)代理服务器! Nginx在功能实现上都采用模块化结构设计 ...

  3. vue2.0的瀑布流组件-使用说明

    做一个小项目,需要瀑布流,就选他了,先看看效果 使用瀑布流布局组件:vue-waterfall-easy 下载引入: 方式一:直接从git上复制组件的完整代码,引入vue组件文件即可 import v ...

  4. jQuery 文档操作方法

    jQuery 文档操作方法 这些方法对于 XML 文档和 HTML 文档均是适用的,除了:html(). 方法 描述 addClass() 向匹配的元素添加指定的类名. after() 在匹配的元素之 ...

  5. python爬虫爬取人人车(二手车)、利用padas、matplotlib生成图表,将信息打成csv格式

    该程序主要为了抓取人人车卖车信息,包括车系.车型号.购车日期.卖车价格.行驶路程.首付价格等等信息.话不多说直接代码. 入库之后将Mongodb里的信息导出成Excel语句 mongoexport - ...

  6. ffempg支持文件解码

    在做一个数据通道 要求有两个 1.支持打开实时流,解码得到图片 2.支持打开视频文件,得到解码图片 第一个要求前任已经实现 bool FfmpegStreamChr::Open(const char ...

  7. Java 测试驱动开发--“井字游戏” 游戏实战

    TDD 介绍 TDD是测试驱动开发(Test-Driven Development)的英文简称,是敏捷开发中的一项核心实践和技术,也是一种设计方法论.TDD的原理是在开发功能代码之前,先编写单元测试用 ...

  8. hadoop+hive+spark搭建(二)

    上传hive软件包到任意节点 一.安装hive软件 解压缩hive软件包到/usr/local/hadoop/目录下 重命名hive文件夹 在/etc/profile文件中添加环境变量 export ...

  9. webapi框架搭建-创建项目(一)

    本文只是一些基本的vs操作,供初学者参考,有基础的请查看 创建项目(二) 创建项目(三) 前言 为了从头了解webapi的技术,创建一个为空的项目 步骤 我用的是vs2017,从文件-->新建- ...

  10. 初识Hibernate的主配置和映射配置

    Hibernate.cfg.xml 主配置 Hibernate.cfg.xml 主配置文件夹中主要配置:数据库链接配置,其他参数配置,映射信息等. 常用配置查看源码: hibernate-distri ...