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 ...
随机推荐
- c++基础知识-数据类型
1.每次新建项都可需写内容 #include <iostream> using namespace std; int main() //main函数有且只有一个 { system(&quo ...
- python-django-ORM模型
ORM模型: Object Relational Mapping 对象关系映射 配置引擎的时候最好直接复制: DATABASES = { 'default': { 'ENGINE': 'django. ...
- python 新闻管理系统——启示
mysql取整函数: mysql函数ceil.floor.round mysql 取整 1.ceil() / ceiling() 向上取整 ex: ceil(1.2) = 2 2.floor() 向下 ...
- Selenium-IDE,在网页上模拟人的操作
想偷懒,不想做很机械重复的网页操作,就百度了一下看看有什么方法,能把自己从重复性的网页操作中解放出来,于是,百度到了selenium ide,折腾许久,用最新版火狐添加了自带selenium ide组 ...
- LetNet、Alex、VggNet分析及其pytorch实现
简单分析一下主流的几种神经网络 LeNet LetNet作为卷积神经网络中的HelloWorld,它的结构及其的简单,1998年由LeCun提出 基本过程: 可以看到LeNet-5跟现有的conv-& ...
- MapReduce05 框架原理OutPutFormat数据输出
目录 4.OutputFormat数据输出 OutputFormat接口实现类 自定义OutputFormat 自定义OutputFormat步骤 自定义OutputFormat案例 需求 需求分析 ...
- Portrait Photography Beginners Guide
Please visit photoandtips稻糠亩 for more information. 六级/考研单词: vogue, derive, gorgeous, thereby, strict ...
- DBeaver客户端工具连接Hive
目录 介绍 下载安装 相关配置 1.填写主机名 2.配置驱动 简单使用 主题设置 字体背景色 介绍 在hive命令行beeline中写一些很长的查询语句不是很方便,急需一个hive的客户端界面工具 D ...
- ES6必知,变量的结构赋值。
对象和数组时 Javascript 中最常用的两种数据结构,由于 JSON 数据格式的普及,二者已经成为 Javascript 语言中特别重要的一部分. 在编码过程中,我们经常定义许多对象和数组,然后 ...
- hadoop基本命令(转)
在这篇文章中,我们默认认为Hadoop环境已经由运维人员配置好直接可以使用. 假设Hadoop的安装目录HADOOP_HOME为/home/admin/hadoop. 启动与关闭 启动HADOOP 进 ...