11039 - Building designing
| Building designing |
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
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; const int MAXN = ;
int t, postive[MAXN], negative[MAXN]; bool cmp2(int a, int b)
{
return a < b;
} int main()
{
cin >> t;
while(t--)
{
memset(postive, , sizeof(MAXN));
memset(negative, , sizeof(MAXN));
int j = , k = ;
int n, res = , tag = ;
cin >> n;
for(int i = ; i <= n; ++i)
{
int cur;
cin >> cur;
if(cur > )
{
postive[j++] = cur;
}
else if(cur < )
{
negative[k++] = -cur;
}
} sort(postive, postive+j, cmp2);
sort(negative, negative+k, cmp2);
int l = ,m = ;
int Max;
if(negative[] < postive[])
{
tag = ;
Max = negative[];
res++;
m++;
}
else
{
tag = ;
Max = postive[];
res++;
l++;
} for(int i = ; i <= n; ++i)
{
if(tag == )
{
while((m < k) && (tag == ))
{
if(negative[m] > Max)
{
res++;
Max = negative[m];
tag = ;
}
m++;
}
}
else if(tag == )
{
while((l < j) && (tag == ))
{
if(postive[l] > Max)
{
res++;
Max = postive[l];
tag = ;
}
l++;
}
} } cout << res << endl;
}
return ;
}
11039 - Building designing的更多相关文章
- 贪心水题。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 贪心
题目链接:UVA - 11039 题意描述:建筑师设计房子有两条要求:第一,每一层楼的大小一定比此层楼以上的房子尺寸要大:第二,用蓝色和红色为建筑染色,每相邻的两层楼不能染同一种颜色.现在给出楼层数量 ...
- UVA 11039 - Building designing(DP)
题目链接 本质上是DP,但是俩变量就搞定了. #include <cstdio> #include <cstring> #include <algorithm> u ...
- UVa 11039 - Building designing
题目大意:n个绝对值各不相同的非0整数,选出尽量多的数,排成一个序列,使得正负号交替且绝对值递增. 分析:按照绝对值大小排一次序,然后扫描一次,顺便做个标记即可. #include<cstdio ...
- UVa 11039 - Building designing 贪心,水题 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVa 11039 Building designing (贪心+排序+模拟)
题意:给定n个非0绝对值不相同的数,让他们排成一列,符号交替但绝对值递增,求最长的序列长度. 析:我个去简单啊,也就是个水题.首先先把他们的绝对值按递增的顺序排序,然后呢,挨着扫一遍,只有符号不同才计 ...
- UVA 11039 - Building designing 水题哇~
水题一题,按绝对值排序后扫描一片数组(判断是否异号,我是直接相乘注意中间值越界)即可. 感觉是让我练习sort自定义比较函数的. #include<cstdio> #include< ...
- UVa 11039 (排序+贪心) Building designing
白书上的例题比较难,认真理解样例代码有助于提高自己 后面的练习题相对简单,独立思考解决问题,增强信心 题意:n个绝对值各不相同的非0整数,选出尽量多的数排成序列,使得该序列正负交错且绝对值递增. 解法 ...
- Building designing UVA - 11039
先取正的和负的绝对值较小者为开头 .然后交替从正负数中取绝对值最小但比上一个大的. 证明: 1.开头选正负数中绝对值较小的:否则能再多放1个. 2.交替选的时候选最小的符合条件的:如果大的符合,换小的 ...
随机推荐
- js递归
先从外层往里调,再反. 要想明白,必须明白执行过程. 如果再不理解,就看函数功能. 函数里自己调自己就是递归!
- 苹果应用 Windows 申请 普通证书 和Push 证书 Hbuilder 个推
最近使用Hbuilder 进行了HTML5开发,因为 HTML5 可以放在android 机器上,也可以放到 IOS机器上,所以很感兴趣,于是开发了一个小应用, 不过问题接着来了: 图1 如图所示:当 ...
- shared_ptr 和 unique_ptr
c++11标准废除乐auto_ptr, C++ 标准库智能指针 使用这些智能指针作为将指针封装为纯旧 C++ 对象 (POCO) 的首选项. unique_ptr 只允许基础指针的一个所有者. 除非你 ...
- 理解Miller-Rabbin算法
转自:http://www.dxmtb.com/blog/miller-rabbin/ 普通的素数测试我们有O(√ n)的试除算法.事实上,我们有O(slog³n)的算法. 定理一:假如p是质数,且( ...
- Thinkphp3.2中的模板继承
1:模板继承: 是3.1.2版本添加的一项更加灵活的模板布局方式,模板继承不同于模板布局,甚至来说,应该在模板布局的上层.模板继承其实并不难理解,就好比 类的继承一样,模板也可以定义一个基础模板( ...
- LoadRunner函数
一.基础函数简介 在VU左边导航栏中,有三个LoadRunner框架函数,分别是vuser_init().Action().vuser_end().这三个函数存在于任何Vuser类型的脚本中. vus ...
- 使用drozer连接时提示:Could not find java. Please ensure that it is installed and on your path
在安装drozer后使用 drozer.bat console connect命令提示如下错误(实际上我已经安装了jdk并添加了path) 参考上面的链接已经它的提示解决方法如下: 建立名为 .dro ...
- Delphi集合的用法
参考:http://www.cnblogs.com/doit8791/archive/2012/08/17/2644859.html 集合是Pascal特有的数据类型,在Visual Basic.C/ ...
- mathematica练习程序(图像取反)
代码很简单,就四行,我想到可以用mathematica干点什么了. 有人通过mathematica编程研究过视频编解码算法么,挺有意思,可以尝试一下. img=Import["f:/lena ...
- Windows硬件断点-实现单步异常
触犯单步异常 改变的是当前Eflags 而不是触发异常的Eflags 也就是 PUSHF MOV EAX, DWORD PTR[ESP] OR EAX, 0x100 MOV D ...