uva11039 设计建筑物
来源:互联网 发布:linux vi创建c文件 编辑:程序博客网 时间:2024/06/10 01:14
题目:
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
题意:
有n个绝对值各不相同的非0整数,选出尽可能多的数,排成一列,使得正负号交替,且绝对值递增。输入整数n和n个整数,输出最长序列长度。
分析:
先依照绝对值从小到大排序,记录选出的序列最后一个的符号,正负交替时ans加1.
ac代码:
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<cmath>using namespace std;const int maxn=5e5+5;int a[maxn];bool cmp(int a,int b){ return abs(a)<abs(b);}int main(){ //freopen("test.txt","r",stdin); int cas; scanf("%d",&cas); while(cas--) { int n; scanf("%d",&n); memset(a,0,sizeof(a)); for(int i=1;i<=n;i++) scanf("%d",&a[i]); sort(a+1,a+n+1,cmp); int change,ans=1; if(a[1]<0) change=-1; else change=1; for(int i=2;i<=n;i++) { if(a[i]*change<0) { ans++; change=-change; } } printf("%d\n",ans); }}
- uva11039 设计建筑物
- UVA11039
- uva11039
- UVa11039
- uva11039(贪心)
- 简单描述,预先设计的建筑物的好处
- 空投建筑物
- Uva11039 Building design...
- uva11039-Building designing
- uva11039 Building designing
- UVA11039 Building Designing
- UVA11039- Building designing
- uva11039 -Building designing
- UVA11039 Building designing
- 贪心--uva11039 building designing
- UVA11039 Building designing (贪心)
- vue+webpack实现精美游戏设计:实现建筑物的渐变生成效果
- 建筑物防雷基本知识
- 设置单机防火墙的一个实例
- hive建表以及测试
- Elasticsearch-4种内置分析器
- iOS中的常用数据持久化
- MarkDownPad-Pro体验
- uva11039 设计建筑物
- 今天在php中把==写成了=
- hibernate,spring,struts面试题
- md5算法详解
- 深入理解Android内核设计思想——读书笔记
- 仿微信公众平台登录页面
- XTU 1206 Dormitory's Elevator
- 使用MAT查看Android内存泄露
- UVA 11014Make a Crystal