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 ...
随机推荐
- lilypond和弦及其转位的表示
在lilypond,如果要打和弦的话,有所谓的chordmode,命令就是\chordmode {} 要使用chordmode需要一些基本的和弦命名的知识,最好先补一下乐理 实际上lilypond的官 ...
- AgileConfig-1.5.5 发布 - 支持 JSON 编辑模式
本次更新加入了2个新的编辑模式:JSON 编辑模式.TEXT 编辑模式.特别是 JSON 编辑模式是大家比较期待的一个功能.因为大家都习惯了 appsettings.json 的配置编辑模式,所以天生 ...
- es使用java的api操作
基本环境的创建 pom依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&q ...
- [spring-core]作用域
本文试图从原理上讲解Spring IoC容器的作用域机制,建议对着源码阅读,事半功倍. 0 引入问题 当我们谈到Spring作用域的时候,自然而然会想到如下作用域(来自spring-core官方文档) ...
- while,do...while及for三种循环结构
循环结构 while循环 while (布尔表达式) { //循环内容 } 只要布尔表达式为true循环就会一直执行 我们大多数情况会让循环停止下来,需要一个让表达式失效的方式来停止循环 while循 ...
- 制作nc文件(Matlab)
首先看一个nc文件中包含哪些部分,例如一个标准的 FVCOM 输入文件 wind.nc: netcdf wind { dimensions: nele = 36858 ; node = 18718 ; ...
- sar 系统活动情况报告
sar(System Activity Reporter系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告, 包括:文件的读写情况.系统调用的使用 ...
- 用jquery的prop方法操作checkbox
prop设置checkbox选中 $('#checkbox-id').prop("checked",true) 判断checkbox是否选中,if ($('#checkbox-id ...
- CSS区分Chrome和Firefox
CSS区分Chrome和FireFox 描述:由于Chrome和Firefox浏览器内核不同,对CSS解析有差别,因此常会有在两个浏览器中显示效果不同的问题出现,解决办法如下: /*Chrome*/ ...
- Mapreduce中的join操作
一.背景 MapReduce提供了表连接操作其中包括Map端join.Reduce端join还有半连接,现在我们要讨论的是Map端join,Map端join是指数据到达map处理函数之前进行合并的,效 ...