UVA 11039-Building designing【贪心+绝对值排序】
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【贪心+绝对值排序】的更多相关文章
- UVA 11039 Building designing 贪心
题目链接:UVA - 11039 题意描述:建筑师设计房子有两条要求:第一,每一层楼的大小一定比此层楼以上的房子尺寸要大:第二,用蓝色和红色为建筑染色,每相邻的两层楼不能染同一种颜色.现在给出楼层数量 ...
- UVa 11039 - Building designing 贪心,水题 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- 贪心水题。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 ...
- UVA 11039 - Building designing 水题哇~
水题一题,按绝对值排序后扫描一片数组(判断是否异号,我是直接相乘注意中间值越界)即可. 感觉是让我练习sort自定义比较函数的. #include<cstdio> #include< ...
- UVa 11039 Building designing (贪心+排序+模拟)
题意:给定n个非0绝对值不相同的数,让他们排成一列,符号交替但绝对值递增,求最长的序列长度. 析:我个去简单啊,也就是个水题.首先先把他们的绝对值按递增的顺序排序,然后呢,挨着扫一遍,只有符号不同才计 ...
- UVa 11039 - Building designing
题目大意:n个绝对值各不相同的非0整数,选出尽量多的数,排成一个序列,使得正负号交替且绝对值递增. 分析:按照绝对值大小排一次序,然后扫描一次,顺便做个标记即可. #include<cstdio ...
- UVA 11039 - Building designing(DP)
题目链接 本质上是DP,但是俩变量就搞定了. #include <cstdio> #include <cstring> #include <algorithm> u ...
- 11039 - Building designing
Building designing An architect wants to design a very high building. The building will consist o ...
- HDOJ2020绝对值排序
绝对值排序 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
随机推荐
- APP安全--网络传输安全 AES/RSA/ECC/MD5/SHA
移动端App安全如果按CS结构来划分的话,主要涉及客户端本身数据安全,Client到Server网络传输的安全,客户端本身安全又包括代码安全和数据存储安全.所以当我们谈论App安全问题的时候一般来说在 ...
- NOIP2017day1游记
NOIP 2017总结 Day1 Day1T1 第一眼看到瞬间慌掉,woc这玩意啥! 然后懵逼了两分钟 好的 我相信他是NOIP第一题 那我就打个表吧 然后花五分钟打了个暴力 玩了几组数据 哇!好像有 ...
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
- Java 管程解决生产者消费者问题
同样是实验存档.//.. 依然以生产者消费者问题作为背景. 管程(=“资源管理程序”)将资源和对资源的操作封装起来,资源使用者通过接口操作资源就 ok,不用去考虑进程同步的问题. 管程: packag ...
- Docker(十):Docker安全
1.Docker安全主要体现在如下方面 a)Docker容器的安全性 b)镜像安全性 c)Docker daemon安全性 2.安装策略 2.1 Cgroup Cgroup用于限制容器对CPU.内存的 ...
- Matplotlib初体验
为一个客户做了关于每个差异otu在时间点上变化的折线图,使用python第一次做批量作图的程序,虽然是很简单的折线图,但是也是第一次使用matplotlib的纪念. ps:在第一个脚本上做了点小的改动 ...
- 程序包管理rpm、yum与简单编译安装程序
Linux程序包管理 Linux中软件的安装主要有两种形式:一种是直接下载源代码包自行编译后安装,另一种直接获取rpm软件包进行安装. 程序的组成部分: 二进制程序:程序的主体文件,比如我们运行一个l ...
- Java Web高级编程(三)
使用过滤器改进应用程序 一.过滤器的目的 过滤器是可以拦截访问资源的请求.资源的响应或者同时拦截两者的应用组件.过滤器可以检测和修改请求和响应,同时也可以拒绝.重定向或转发请求.javax.servl ...
- px-rem自适应转换
当前团队开发过程,存在2种度量单位(px.rem)各有说辞px:各个终端统一大小,简单明了,未尝不可!rem:大屏幕显示大字体,小屏幕显示小字体,渐进增强视觉感.业界各种写法都有,不逐一讨论.团队呼声 ...
- Java学习笔记5(类的入门以及ArrayList)
1.类的概念:将现实生活中的事物抽象成了代码(类),我们可以使用自定义的数组类型(类)来描述现实生活中的事物. 2.分析:用一部手机来分析,手机可以打电话,上网,听音乐,这些就是方法,手机有型号,颜色 ...