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语言

    今天搞了一个多小时,头是疼的,应该是没休息好吧,学习了数据结构这一节,感觉收益良多,下面贴上代码和心得: /*24_链表创建和链表遍历算法的演示*/ # include <stdio.h> ...

  2. java 设计模式-缺省适配器模式

    本文转载地址:http://www.cnblogs.com/iyangyuan/archive/2013/03/11/2954808.html 在程序设计过程中,读者很可能遇到这样一种困境:设计了一个 ...

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

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

  4. laravel框架一种方便的快速填充数据的方法

    首先大家都知道在laravel框架里是采用seeder来填充数据的,具体命令如下,请将如下的类名称替换成你具体的seeder类名. 首先创建seeder类 php artisan make:seede ...

  5. install pytorch

    1. install and update pip3 2. install numpy and scipy 3. install pytorch

  6. IDEA 环境设置

    IDEA环境设置 任何事物都有两面性,如何用好才是关键.IDEA为我们提供了丰富的功能,但不代表默认的配置就适合于你.我们应当根据自己的条件.需求合理的配置,从而驾驭好这匹悍马.让它成为我们编程的利器 ...

  7. ubuntu12.04 安装中文输入法

    1.  安装输入法的第一步,是安装语言包.我们选择System Settings-->Language Support-->Install/Remove Languages 选择中文 2. ...

  8. php消息队列之 think queue消息队列初体验

    使用thinkphp 5的  消息队列 think queue ● php think queue:listen --queue queuename ● php think queue:work -- ...

  9. C#语言和SQL Server第八章笔记

    一:                                                                                                   ...

  10. C#语言和SQL Server 数据库处理

    ---恢复内容开始--- 第七章 用表组织数据 1:数据性分类: 1>实体完整性的约束:检验每行数据是否符合要求 检验每列数据是否符合要求 2>域完整性约束:给定列输入的有效性 3> ...