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. IOS学习4——block代码块

    本文转载自:iOS开发-由浅至深学习block 一.关于block 在iOS 4.0之后,block横空出世,它本身封装了一段代码并将这段代码当做变量,通过block()的方式进行回调.这不免让我们想 ...

  2. 将自己的域名代理到Gitpages

    相信有很多程序员都有自己的域名,甚至很多人还有自己的服务器.去年我也买了半年的阿里云,在tomcat里面发war包,相当于一个正式的项目.但是很多前端程序员应该要求很简单,就是能将静态的html发布就 ...

  3. Python3.5:装饰器的使用

    在Python里面函数也是一个对象,而且函数对象可以被赋值给变量,所以,通过变量也能调用该函数,简单来说函数也是变量也可以作文函数的参数 >>> def funA(): ... pr ...

  4. Intellj Idea使用tomcat部署不成功,死活也找不到解决办法的看这里

    Intellij 周六晚上开发一个简单web项目的,使用tomcat打包部署,死活也没法部署成功,和这个问题怼了6个小时,也没搞清楚具体为什么不能访问页面,但是好在最后还是找了个方法把问题解决了.以下 ...

  5. Zabbix自动发现java进程

    一:简介 使用Python psutil模块,查找java模块,并获取启动命令,结合zabbix监控自动监控.点击下载 二:操作 发现脚本 #!/usr/bin/env python # coding ...

  6. ab返回结果参数分析

    Server Software    返回的第一次成功的服务器响应的HTTP头.Server Hostname    命令行中给出的域名或IP地址Server Port    命令行中给出端口.如果没 ...

  7. SpringBoot_02_servlet容器配置

    二.参考资料 1.Spring boot 自定义端口 2.Spring Boot的Web配置(九):Tomcat配置和Tomcat替换

  8. 【liferay】2、可配置portlet

    定义:edit和config模式一般没有使用,对于使用editor和config等模式的portlet,我们可以将他们称为可配置portlet. 我们先新建一个portlet项 添加可配置的控制元素, ...

  9. Python核心编程笔记--私有化

    一.私有化的实现 在Python中想定义一个类是比较简单的,比如要定义一个Person类,如下代码即可: # -*- coding: utf-8 -*- # __author : Demon # da ...

  10. dlib人脸关键点检测的模型分析与压缩

    本文系原创,转载请注明出处~ 小喵的博客:https://www.miaoerduo.com 博客原文(排版更精美):https://www.miaoerduo.com/c/dlib人脸关键点检测的模 ...