A. Be Positive
A. Be Positive
1 second
256 megabytes
standard input
standard output
You are given an array of nn integers: a1,a2,…,ana1,a2,…,an. Your task is to find some non-zero integer dd (−103≤d≤103−103≤d≤103) such that, after each number in the array is divided by dd, the number of positive numbers that are presented in the array is greater than or equal to half of the array size (i.e., at least ⌈n2⌉⌈n2⌉). Note that those positive numbers do not need to be an integer (e.g., a 2.52.5 counts as a positive number). If there are multiple values of dd that satisfy the condition, you may print any of them. In case that there is no such dd, print a single integer 00.
Recall that ⌈x⌉⌈x⌉ represents the smallest integer that is not less than xx and that zero (00) is neither positive nor negative.
Input
The first line contains one integer nn (1≤n≤1001≤n≤100) — the number of elements in the array.
The second line contains nn space-separated integers a1,a2,…,ana1,a2,…,an (−103≤ai≤103−103≤ai≤103).
Output
Print one integer dd (−103≤d≤103−103≤d≤103 and d≠0d≠0) that satisfies the given condition. If there are multiple values of dd that satisfy the condition, you may print any of them. In case that there is no such dd, print a single integer 00.
Examples
input
5
10 0 -7 2 6
output
4
input
7
0 0 1 -1 0 0 2
output
0
Note
In the first sample, n=5n=5, so we need at least ⌈52⌉=3⌈52⌉=3 positive numbers after division. If d=4d=4, the array after division is [2.5,0,−1.75,0.5,1.5][2.5,0,−1.75,0.5,1.5], in which there are 33 positive numbers (namely: 2.52.5, 0.50.5, and 1.51.5).
In the second sample, there is no valid dd, so 00 should be printed.
题解:计算出正数和负数的个数,如果正数大于(n+1)/2的话,就是随便输出一个正数,如果负数大于(n+1)/2的话,就随便输出一个负数,否则输出0.
#include<iostream>
#include<cstdio> using namespace std; int main()
{
int n,a[],cnt=,cnt2=;
cin>>n;
for(int i=;i<n;i++)
{
scanf("%d",a+i);
if(a[i]>) //找出正数的个数
cnt++;
else if(a[i]<) //找出负数的个数
cnt2++; }
if(cnt>=(n+)/)
printf("1\n");
else if(cnt2>=(n+)/)
printf("-1\n");
else
printf("0\n");
return ;
}
A. Be Positive的更多相关文章
- [LeetCode] First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- Leetcode First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- Interleaving Positive and Negative Numbers
Given an array with positive and negative integers. Re-range it to interleaving with positive and ne ...
- 【leetcode】First Missing Positive
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- 【leetcode】First Missing Positive(hard) ☆
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- LeetCode - 41. First Missing Positive
41. First Missing Positive Problem's Link ---------------------------------------------------------- ...
- LeetCode题解-----First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- hdu 5183. Negative and Positive (哈希表)
Negative and Positive (NP) Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- Java for LeetCode 041 First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
- [LintCode] Interleaving Positive and Negative Numbers
Given an array with positive and negative integers. Re-range it to interleaving with positive and ne ...
随机推荐
- IDEA Git回退到指定历史版本
1.找到要回退的版本号(右击项目--> Git --> Show History -->选中要回退的版本-->Copy Revision Number): 2.打开idea的T ...
- 从入门到自闭之Python字典如何使用
字典: 定义:dict dict = {"key":"value"} -- 键值对 作用:存储大量数据,数据和数据起到关联作用 所有的操作都是通过键来完成 键: ...
- Python 入门 之 包
Python 入门 之 包 1.包 (1)什么是包? 文件夹下具有_ init.py _的文件夹就是一个包 (2)包的作用: 管理模块(文件化) (3)包的导入: 导入: 启动文件要和包文件是同级 绝 ...
- Zabbix 配置钉钉脚本告警
1.钉钉账号创建,并创建一个组,在组中添加一个机器人,然后记下webhook地址即可. 2.编辑一个报警脚本,此处使用的是BASH脚本,并覆盖我们的webhook地址到相应的位置. [root@loc ...
- git 常用命令语句(个人笔记)
切换账户 git config user.name xxxxx 查看用户名 ex: git config user.name tongjiaojiao git config user.e ...
- Visual Studio (VC) Win32 程序由于数据大,内存溢出怎么办?
Visual Studio (VC) 内编写的Win32 程序由于数据大,内存溢出,即使转移到64位系统也不行.在国外网站上找到了答案. 原来,只需在project->property中的Lin ...
- Linux上安装postgres 10.5
由于接触了华为的elk大数据平台,里面封装的是postgres ,就想着安装一下,熟悉一下postgres数据. 安装包下载:https://www.postgresql.org/ftp/source ...
- Spark写入HBase(Bulk方式)
在使用Spark时经常需要把数据落入HBase中,如果使用普通的Java API,写入会速度很慢.还好Spark提供了Bulk写入方式的接口.那么Bulk写入与普通写入相比有什么优势呢? BulkLo ...
- MySQL中导出用户权限设置的脚本
在对MySQL数据库进行迁移的时候,有时候也需要迁移源数据库内的用户与权限.对于这个迁移我们可以从mysql.user表来获取用户的相关权限来生成相应的SQL语句,然后在目标服务器上来执行生成的SQL ...
- CDH5.16.1的Yarn提交任务默认资源分配
1 同时运行5个Spark任务的资源分配截图 2 每个任务占用3个Container 3个core以及4.5GB内存 也就是说一个Container需要 1个core 以及 512MB的内存 如果资源 ...