博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #181 (Div. 2) A. Array 构造
阅读量:5168 次
发布时间:2019-06-13

本文共 2581 字,大约阅读时间需要 8 分钟。

A. Array

题目连接:

Description

Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold:

The product of all numbers in the first set is less than zero ( < 0).

The product of all numbers in the second set is greater than zero ( > 0).
The product of all numbers in the third set is equal to zero.
Each number from the initial array must occur in exactly one set.
Help Vitaly. Divide the given array.

Input

The first line of the input contains integer n (3 ≤ n ≤ 100). The second line contains n space-separated distinct integers a1, a2, ..., an (|ai| ≤ 103) — the array elements.

Output

In the first line print integer n1 (n1 > 0) — the number of elements in the first set. Then print n1 numbers — the elements that got to the first set.

In the next line print integer n2 (n2 > 0) — the number of elements in the second set. Then print n2 numbers — the elements that got to the second set.

In the next line print integer n3 (n3 > 0) — the number of elements in the third set. Then print n3 numbers — the elements that got to the third set.

The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them.

Sample Input

3

-1 2 0

Sample Output

1 -1

1 2

1 0

Hint

题意

给你n个数,然后让你分成三组,要求第一组的乘积是小于0的,第二组是大于0的,第三组是等于0的

让你输出一个方案

题解:

第一组需要奇数个负数,第二组需要偶数个负数

如果负数是偶数,那么扔一个去第三组,那么就可以变成奇数了

奇数 = 奇数+偶数

所以讨论一下就好了

代码

#include
using namespace std;vector
ans[4];int a[400];int main(){ int n; scanf("%d",&n); int num = 0; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); if(a[i]<0)num++; } if(num==1) { for(int i=1;i<=n;i++) { if(a[i]<0)ans[0].push_back(a[i]); else if(a[i]==0)ans[2].push_back(a[i]); else ans[1].push_back(a[i]); } } else if(num%2==0) { int k = 0; for(int i=1;i<=n;i++) { if(a[i]<0&&k==0){ans[0].push_back(a[i]);k++;} else if(a[i]<0&&k==1){ans[2].push_back(a[i]);k++;} else if(a[i]==0)ans[2].push_back(a[i]); else ans[1].push_back(a[i]); } } else { int k = 0; for(int i=1;i<=n;i++) { if(a[i]<0&&k==0){ans[0].push_back(a[i]);k++;} else if(a[i]==0)ans[2].push_back(a[i]); else ans[1].push_back(a[i]); } } for(int i=0;i<3;i++) { printf("%d ",ans[i].size()); for(int j=0;j

转载于:https://www.cnblogs.com/qscqesze/p/5118538.html

你可能感兴趣的文章
oracle数据投毒,Oracle Database Server TNS Listener远程数据投毒漏洞(CVE-2012-1675)的完美解决方法...
查看>>
oracle创建函数难点,oracle创建函数遇到的坑,
查看>>
PHP抽象函数的依赖注入,依赖注入_PHP编程_互联网开发技术网_传播最新的编程技术_php361.com...
查看>>
linux下创建nginx虚拟目录详解,配置Nginx服务器虚拟目录
查看>>
嵌入式linux内核gpio使用,嵌入式Linux下面查看gpio使用情况
查看>>
利用linux命令进行入侵检测分析总结,Linux之入侵痕迹清理总结
查看>>
linux 文件命名空间,Linux 命名空间
查看>>
Block的本质
查看>>
Pixel 3a 开箱及 Google Fi 服务评测视频
查看>>
用一个div模拟textarea的实现【前端每日一题-15】
查看>>
Python学习教程:爬虫分析了京东内衣销售记录,哪个size最多?
查看>>
有Excel、Tableau、PowerBI都能做数据分析,为什么还要用Python
查看>>
redis如何进行分库存储和选择模糊清除缓存
查看>>
spring security退出方法
查看>>
从获得字符串中获取数字
查看>>
传入一个月份获取该月的统计信息
查看>>
分组取出值最大的数据
查看>>
java判断为空的方法
查看>>
double类型的数值转为小数点2位
查看>>
java比较两个时间年月份的大小
查看>>