CF1130A Be Positive 题解
Content
有一个长度为 \(n\) 的数组 \(a_1,a_2,a_3,...,a_n\),试找出一个数 \(d\),使得数组中的每个数除以 \(d\) 得到的 \(n\) 个结果中至少有 \(\dfrac{n}{2}\) 个正数,输出任意一个 \(d\) 均可,或者没有这样的 \(d\)。
数据范围:\(1\leqslant n\leqslant 100,-10^3\leqslant a_i\leqslant 10^3\)。
Solution
先统计一下这里面有多少个正数和多少个负数。如果正数和负数的数量都没有到 \(\left\lceil\dfrac{n}{2}\right\rceil\) 的话就不存在这样的 \(d\),否则看正数和负数的数量哪个有 \(\left\lceil\dfrac{n}{2}\right\rceil\),如果是正数多除数就应该是正数,否则就应该是负数,这里随便输出什么都行能得到答案就好,我的代码中正数多输出的是 \(1\),负数多输出的是 \(-1\)。
Code
int n, a[100007], pos, neg;
int main() {
getint(n);
_for(i, 1, n) {
getint(a[i]);
pos += (a[i] > 0), neg += (a[i] < 0);
}
if(pos >= (n % 2 ? n / 2 + 1 : n / 2)) printf("1");
else if(neg >= (n % 2 ? n / 2 + 1 : n / 2)) printf("-1");
else printf("0");
return 0;
}
CF1130A Be Positive 题解的更多相关文章
- LeetCode Find Duplicate File in System
原题链接在这里:https://leetcode.com/problems/find-duplicate-file-in-system/description/ 题目: Given a list of ...
- LeetCode Reshape the Matrix
原题链接在这里:https://leetcode.com/problems/reshape-the-matrix/#/description 题目: In MATLAB, there is a ver ...
- LeetCode题解-----First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- [LeetCode]题解(python):041-First Missing Positive
题目来源 https://leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the ...
- LeetCode题解41.First Missing Positive
41. First Missing Positive Given an unsorted integer array, find the first missing positive integer. ...
- 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
1.问题描述 2.题解思路 本题的思路是对于数组中每个正的元素,应该将其放到数组中对应的位置,比如元素1 ,应该放在数组的第一个位置.以此类推,最后检查数组中元素值和下标不匹配的情况. 3.代码 in ...
- [LeetCode 题解]: First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
随机推荐
- excel的表格数据插入到数据库
看到有excel保存insert的数据,自己照着教程弄了一下,可以的. 表格数据 接下来我们在d3的位置输入 =CONCATENATE("insert into user(code, nam ...
- java番外茶余饭后闲聊
java番外茶余饭后闲聊 **本人博客网站 **IT小神 www.itxiaoshen.com 今天聊点题外话没事时可以作为平时沟通交流的谈资,接下来一起简单了解下个人知晓对Java界开发产生深远影响 ...
- 状压DP详解+题目
介绍 状压dp其实就是将状态压缩成2进制来保存 其特征就是看起来有点像搜索,每个格子的状态只有1或0 ,是另一类非常典型的动态规划 举个例子:有一个大小为n*n的农田,我们可以在任意处种田,现在来描述 ...
- Git常用操作(二)
仓库拉取 git clone XXX 修改仓库链接 $ git config -l # 显示coding列表 $ git config --get remote.origin.url # 返回orig ...
- 硬盘SSD、HDD和SSHD都是什么意思?哪种类型硬盘最好?
硬盘分类:(1)HHD 机械硬盘(Mechanical hard disk)(2)SSD 固态硬盘(solid state drive/disk)(3)SSHD 混合硬盘,说白了就是HDD+SSD=S ...
- Linux软件安装管理:rpm与yum
目录 1. rpm包的管理 1.1 介绍 1.2 rpm包的简单查询指令 1.3 rpm 包名的基本格式 1.4 rpm其它指令 1.5 卸载rpm包 1.6 安装rpm包 2. yum 2.1 说明 ...
- php操作mongodb手册地址
php操作mongodb手册地址: http://php.net/manual/zh/class.mongocollection.php
- 容器之分类与各种测试(四)——multimap
multiset和multimap的具体区别在于,前者的key值就是自己存储的value,后者的key与value是分开的不相关的. 例程 #include<stdexcept> #inc ...
- ORACLE 查询sql和存储性能思路
1.确定session id 如果是存储过程,在程序开头设置客户端标识.并根据标识获取session id. DBMS_SESSION.set_identifier('XXX'); select * ...
- awk的基本用法
最近遇到导入的csv文件首行为日期,但需要将日期作为列导入到数据库中,直接使用ctl文件好像无法实现,了解到awk这个强大的命令. 导入的CSV文件除了首行为日期,其他的都是格式相同的.需要将首行单独 ...