uva 11039 Building designing (排序)
来源:互联网 发布:linux ssh端口转发 编辑:程序博客网 时间:2024/06/05 01:54
uva 11039 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
257-269-3811-9251817-154
Sample Output
25
题目大意:n个绝对值各不相同的非0整数,选出尽量多的数,排成一个序列,使得正负号交替且绝对值递增。
#include<stdio.h>#include<math.h>#include<algorithm>using namespace std;int num[500005];int cmp(int a, int b) {return abs(a) < abs(b);}int main() {int T;scanf("%d", &T);while (T--) {int n;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &num[i]);}sort(num, num + n, cmp);int ans = 0, flag = 0;for (int i = 0; i < n; i++) {if (!flag) { //第一个数,flag标记正负if (num[i] > 0) flag = 1;else flag = 2;ans++;continue;}if (flag == 1) { //上一个数为正,当前数为负if (num[i] < 0) {flag = 2;ans++;continue;}}if (flag == 2) { //上一个数为负。当前数为正if (num[i] > 0) {flag = 1;ans++;continue;}}}printf("%d\n", ans);}return 0;}
- uva 11039 Building designing (排序)
- UVA 11039 Building designing
- UVA 11039 - Building designing
- uva 11039 - Building designing
- Uva-11039-Building designing
- UVa 11039 - Building designing
- Uva - 11039 - Building designing
- UVA 11039 - Building designing
- uva 11039 Building designing
- Uva 11039 - Building designing
- UVA 11039 - Building designing
- UVA - 11039 Building designing
- UVA 11039 Building designing
- UVA 11039 Building designing
- UVA - 11039 Building designing
- UVa 11039 - Building Designing
- UVa 11039 - Building designing
- UVA - 11039 Building designing
- C++中的RAII机制
- 谈谈Linux内核驱动的coding style
- Android学习笔记:访问和添加通讯录中的联系人和联系人数据库表简介一
- 使用imageSwitcher左右划查看手机相片
- Android网络编程之WebKit应用
- uva 11039 Building designing (排序)
- UVa 1584 - Circular Sequence
- C++ main函数:处理命令行选项
- (五)CXF整合Spring发布RESTful风格的Web服务
- Java压缩文件和解压缩文件
- 修改struts中:fielderror.ftl 模板
- 10个免费的jQuery可视化编辑器插件
- C/C++ 中的 static
- UVa 11401 Triangle Counting(数学递推)