uva11039 Building Designing

题意:给出n个数,请按照他们绝对值的递增顺序排序,且相邻元素不能有相同符号(必须一个大于0,一个小于0),问这样操作后最多有多少个元素

分析:先调用sort排序然后逐个判断相邻两个元素的乘积是否小于0.  注意结果会溢出,不能用int,可以用double,当然用bool型变量标记也可以

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <vector>
 4 #include <algorithm>
 5 #include <math.h>
 6 #define zz
 7 using namespace std;
 8 bool cmp(double a, double b){
 9     return fabs(a)<fabs(b);
10 }
11 int main(){
12 #ifndef zz
13     freopen("in.txt", "r", stdin);
14 #endif
15     int p;
16     scanf("%d", &p);
17     while(p--){
18         vector<double>v;
19         int n;
20         scanf("%d", &n);
21         int i;
22         for(i=0; i<n; i++){
23             int x;
24             scanf("%d", &x);
25             v.push_back(x);
26         }
27         sort(v.begin(), v.end(), cmp);
28         double jar = v[0];
29         int cnt = 1;
30         for(i=1; i<v.size(); i++){
31             if(v[i]*jar<0){
32                 cnt++;
33                 jar = v[i];
34             }
35         }
36         printf("%d\n", cnt);
37     }
38     return 0;
39 }

 

时间: 2024-10-26 02:12:43

uva11039 Building Designing的相关文章

uva 11039 - Building designing

点击打开链接uva 11039 思路:贪心(排序+枚举) 分析: 1 题目要求按照题目给定的数据求能够建立最高几层的楼房 2 题目要求上层的大小要比下层小,并且两层之间的颜色要交替的出现.很明显,我们只要按照size进行排序,然后枚举即可. 代码: #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int M

Building a T-SQL Loop

November 5, 2003T-SQL Programming Part 2 - Building a T-SQL LoopBy Gregory A. Larsen This is the second article in my T-SQL programming series. This article will discuss building a program loop using T-SQL. In addition to talking about building a loo

创建自定义模板 Building Custom Templates

创建|模板 December 23, 2002Using and Building Query Analyzer TemplatesBy Gregory A. Larsen Building Custom TemplatesBy now you should be somewhat familiar with using pre-existing templates to build your SQL code. It is now time to look at how you can lev

Chapter 4 Building Classes and Assemblies with VB.NET

Building Classes andAssemblies with VB.NETDuring the last three or so years, most VB developers havespent a great percentage of their time building COM compo-nents.These components are used as the middle-tier compo-nentsin n-tier systems. The benefit

Building OpenSSL with Visual Studio

Downloads I provide downloads for Visual Studio 2010 and 2015. I had to remove my 2013 installation due to space constraints, but the build files are still there so you can do it, too. Version Visual Studio 2010 Visual Studio 2015 OpenSSL 1.0.2d 32-B

POJ 3566 (弱校联萌十一大决战之厉兵秣马 B. Building for UN)

Building for UN Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 1788   Accepted: 805   Special Judge Description The United Nations has decided to build a new headquarters in Saint Petersburg, Russia. It will have a form of a rectangular

eclipse启动后项目Building Workspace卡在54%一直不动了

问题描述 eclipse启动后项目Building Workspace卡在54%一直不动了 卡在54%不动了,请问怎么解决啊 解决方案 (1).解决方法 方法1.修改eclipse启动文件 eclipse.ini 中添加启动参数参数: -vmargs -Xmx512m 方法2.关闭自动构建工作区: project -> build Auto-.. 方法3.在eclipse.ini式中添加了一个命令 -clean (2).加大Eclipse运行可用最大内存数 具体操作: 修改位于eclipse目录

Building OpenCASCADE on Debian

Building OpenCASCADE on Debian eryar@163.com Abstract. When you are familiar with OpenCASCADE on Windows, you should try it on Linux operating system, because of Debian is also open source like OpenCASCADE. The paper is focus on how to building the O

Spring Boot 项目构建 之 使用 Spring Boot 构建应用(Building an Application with Spring Boot)

Table of contents What you'll build What you'll need How to complete this guide Build with Gradle Build with Maven Build with Spring Tool Suite Learn what you can do with Spring Boot Create a simple web application Create an Application class Run the