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 题解的更多相关文章

  1. LeetCode Find Duplicate File in System

    原题链接在这里:https://leetcode.com/problems/find-duplicate-file-in-system/description/ 题目: Given a list of ...

  2. LeetCode Reshape the Matrix

    原题链接在这里:https://leetcode.com/problems/reshape-the-matrix/#/description 题目: In MATLAB, there is a ver ...

  3. LeetCode题解-----First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  4. [LeetCode]题解(python):041-First Missing Positive

    题目来源 https://leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the ...

  5. LeetCode题解41.First Missing Positive

    41. First Missing Positive Given an unsorted integer array, find the first missing positive integer. ...

  6. Leetcode 题解 First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  7. leetCode题解之First Missing Positive

    1.问题描述 2.题解思路 本题的思路是对于数组中每个正的元素,应该将其放到数组中对应的位置,比如元素1 ,应该放在数组的第一个位置.以此类推,最后检查数组中元素值和下标不匹配的情况. 3.代码 in ...

  8. [LeetCode 题解]: First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  9. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

随机推荐

  1. linux新增定时脚本

    crontab -e 然后新增: 0 * * * * sh /usr/local/redis/copy/redis_rdb_copy_hourly.sh 控制台回显"crontab:inst ...

  2. 什么是CLI、GUI

    就是命令行界面command-line interface,也有人称之为字符用户界面(CUI). 通常认为,命令行界面(CLI)没有图形用户界面(GUI)那么方便用户操作. 因为,命令行界面的软件通常 ...

  3. 华为云企业级Redis评测第一期:稳定性与扩容表现

    摘要:采用Redis Labs推出的多线程压测工具memtier_benchmark对比测试下GaussDB(for Redis) 和原生Redis的特性差异. 本文分享自华为云社区<华为云企业 ...

  4. [ARC 122]

    最近状态差到爆炸. \(AT\)连掉两把分,啥时候能上黄啊. \(A\) 考虑直接动归. 把\(O(n^2)\)的动归后缀和优化成\(O(n)\) A #include<iostream> ...

  5. 洛谷 P4569 - [BJWC2011]禁忌(AC 自动机+矩阵乘法)

    题面传送门 又好久没做过 AC 自动机的题了,做道练练手罢( 首先考虑对于某个固定的字符串怎样求出它的伤害,我们考虑贪心,每碰到出现一个模式串就将其划分为一段,最终该字符串的代价就是划分的次数.具体来 ...

  6. 每日自动健康打卡(Python+腾讯云服务器)

    每日自动健康打卡(Python+腾讯云服务器) 1.配置需要 python3.7,Chrome或者Edeg浏览器,Chrome驱动或者Edge驱动 #需要配置selenium库,baidu-aip库, ...

  7. hive向mysql导入数据sqoop命令出错

    报错信息: java.lang.Exception: java.io.IOException: java.lang.ClassNotFoundException: info at org.apache ...

  8. Yarn 生产环境核心参数配置案例

    目录 Yarn 生产环境核心参数配置案例 需求 修改yarn-site.xml配置 分发 重启集群 执行WordCount程序 Yarn 生产环境核心参数配置案例 调整下列参数之前要拍摄Linux快照 ...

  9. stlink 无法再keil中识别 按下复位键可以识别

    最近遇到一个很是头痛的问题 本来板子是好好的,就是从公司带回的家里 然后再次用stlink烧写程序的时候就出现了问题: 但是查看电脑端,上面是有stlink的 也就是电脑是好的, 我立刻又试了一下家中 ...

  10. Docker学习(一)——安装docker

    Suse12上安装docker   对于suse13.2之后的版本,因为docker已经被添加到了suse仓库中,直接使用sudo zypper install docker即可.   suse12不 ...