Codeforces Round #181 (Div. 2) A. Array 构造
A. Array
题目连接:
http://www.codeforces.com/contest/300/problem/A
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<bits/stdc++.h>
using namespace std;
vector<int>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<ans[i].size();j++)
printf("%d ",ans[i][j]);
printf("\n");
}
}
Codeforces Round #181 (Div. 2) A. Array 构造的更多相关文章
- Codeforces Round #284 (Div. 1) C. Array and Operations 二分图最大匹配
题目链接: http://codeforces.com/problemset/problem/498/C C. Array and Operations time limit per test1 se ...
- Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】
传送门:http://codeforces.com/contest/1108/problem/E2 E2. Array and Segments (Hard version) time limit p ...
- Codeforces Round #339 (Div. 1) C. Necklace 构造题
C. Necklace 题目连接: http://www.codeforces.com/contest/613/problem/C Description Ivan wants to make a n ...
- Codeforces Round #181 (Div. 2) C. Beautiful Numbers 排列组合 暴力
C. Beautiful Numbers 题目连接: http://www.codeforces.com/contest/300/problem/C Description Vitaly is a v ...
- Codeforces Round #181 (Div. 2) B. Coach 带权并查集
B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...
- Codeforces Round #306 (Div. 2) ABCDE(构造)
A. Two Substrings 题意:给一个字符串,求是否含有不重叠的子串"AB"和"BA",长度1e5. 题解:看起来很简单,但是一直错,各种考虑不周全, ...
- Codeforces Round #298 (Div. 2) D. Handshakes 构造
D. Handshakes Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/problem ...
- Codeforces Round #276 (Div. 2)C. Bits(构造法)
这道题直接去构造答案即可. 对于l的二进制表示,从右到左一位一位的使其变为1,当不能再变了(再变l就大于r了)时,答案就是l. 这种方法既可以保证答案大于等于l且小于等于r,也可以保证二进制表示时的1 ...
- Codeforces Round #181 (Div. 2)
A. Array 模拟. B. Coach 模拟. C. Beautiful Numbers good number的位和最大不超过\(10^7\),那么只要枚举a或b的个数,然后最多循环7次判断位和 ...
随机推荐
- CKEditor和IMCE构建drupal编辑器
1. 安装并启用CKEditor和IMCE模块. 2. 配置CKEditor: 3. 配置用户的权限即可.最终效果图:
- pssh 不能执行指定用户命令
问题: 一个脚本a.sh(必须root用户执行),在本地可以运行,通过pssh -h ip_file "cd /home/byte/a.sh"不能执行. 原因: 分析应该是ssh ...
- JSON 教程学习进度备忘
书签:跳过:另外跳过的内容有待跟进 __________________ 学习资源:W3School. _________________ 跳过的内容: 1. ______________ 知识点:1 ...
- 简易nagios安装出现的问题及解决方法
安装时候所遇到的问题: A 安装PHP的时候报错xml2-config未找到 安装PHP的时候报错:未找到包libxml2 解决方法:安装libxml2包即可,libxml2是php的依赖包,从而需要 ...
- strtol()函数
#include <stdlib.h>#include <stdio.h> int main(){ char a[] = "100"; char b[] = ...
- 指向函数的指针数组(C++)
我们能够创建一个指向函数的指针数组.为了选择一个函数,只需要使用数组的下标,然后间接引用这个指针.这种方式支持表格式驱动码的概念:可以根据状态变量去选择被执行函数,而不用条件语句或case语句.这种设 ...
- java多线程之synchronized(线程同步)
一.线程同步,主要应用synchronized关键字: public class TraditionalThreadSynchorinzed { public static void main(Str ...
- [POJ] #1003# Hangover : 浮点数运算
一. 题目 Hangover Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 116593 Accepted: 56886 ...
- 通过网络方式安装linux的五种方法
在线观看:http://video.sina.com.cn/v/b/43086503-1443650204.html http://video.sina.com.cn/v/b/43095530-144 ...
- 关于网站编码显示问题 效果是 访问 带有中文注释的sass文件出现编码报错。
首先查看环境变量 export declare -x HOME="/home/piperck" declare -x LANG="en_US.UTF-8" de ...