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. 非常详细的 Docker 学习笔记-转载

    文章链接 一.Docker 简介 Docker 两个主要部件: Docker: 开源的容器虚拟化平台 Docker Hub: 用于分享.管理 Docker 容器的 Docker SaaS 平台 --  ...

  2. git push的用法

    git push <远程仓库名> <本地分支名>:<远程分支名>

  3. 学习Object.assign()

    Object.assign()用于将所有可枚举的值从一个或多个源对象复制到目标对象.它将返回目标对象. 语法 Object.assign(target, ...source); var obj = { ...

  4. java操作时间,将当前时间减一年,减一天,减一个月

    在Java中操作时间的时候,常常遇到求一段时间内的某些值,或者计算一段时间之间的天数 Date date = new Date();//获取当前时间 Calendar calendar = Calen ...

  5. 关于sleep函数的一些问题和资料

    //================================================================================================ 2 ...

  6. 基于Java Mail 进行发送(带附件和压缩附件)的邮件

    刚进公司的training, 下面是要求: Self-study of Java Mail library:  http://www.oracle.com/technetwork/java/javam ...

  7. js遍历 子节点 子元素

    Js 节点 子元素 属性 方法 // 添加子节点前 删除所有子节点 var usernameEle = document.getElementById("username"); v ...

  8. MySQL数据库学习02: SELECT语句

    声明:本篇文章大多数内容出自<MySQL必知必会>,仅供学习参考,勿作他用! 第4章 检索数据 4.1 SELECT语句 SELECT子句用于检索数据库中的表数据.它几乎是MySQL中最常 ...

  9. Hyper-V 虚拟网络设置

    目标:搭建一个主机上的网络用来链接主机和虚拟机,并且虚拟机可以通过主机上网. 步骤一:创建一个Internal Network. 步骤二: 创建虚拟机并设置Virtual Switch. 步骤三:将上 ...

  10. java内存溢出问题

    相信有一定java开发经验的人或多或少都会遇到OutOfMemoryError的问题,这个问题曾困扰了我很长时间,随着解决各类问题经验的积累以及对问题根源的探索,终于有了一个比较深入的认识. 在解决j ...