您的位置:首页 > 其它

【codeforces】A. Array(思维水题)

2017-08-02 22:29 549 查看
点击打开题目

A. Array

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

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.

Examples

input
3
-1 2 0


output
1 -1
1 2
1 0


input
4
-1 -2 -3 0


output
1 -1
2 -3 -2
1 0


题意:把输入的数字分割成三种数组,第一种元素之积为负,第二种为正,第三种为0‘

题解:第一种负数的个数为奇数个,多余的给第二个,第二个数组负数为偶数,第三个有0就行。

代码:

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main(){
int n;
while(cin>>n){
int a1[110],a2[110],a3[110];
int k1=0,k2=0,k3=0;
int temp;
memset(a1,0,sizeof(a1));
memset(a2,0,sizeof(a2));
memset(a3,0,sizeof(a3));
for(int i=0;i<n;i++){
cin>>temp;
if(temp<0)
a1[k1++]=temp;
else if(temp==0)
a2[k2++]=temp;
else
a3[k3++]=temp;
}
int flag1,flag2,flag3;
flag1=flag2=flag3=0;
int m1=k1;
int m2=k2;
int m3=k3;
if(!m3){//正数是否为空
a3[0]=a1[m1-1];
a3[1]=a1[m1-2];
m1-=2;
m3+=2;
}
if(m1&1){//判断是否为奇数个负数
cout<<m1<<" ";
for(int i=0;i<m1-1;i++)
cout<<a1[i]<<" ";cout<<a1[m1-1]<<endl;
}
else{//否则的话,减掉一个
a2[m2]=a1[m1-1];
m2++;
m1-=1;
cout<<m1<<" ";
for(int i=0;i<m1-1;i++)
cout<<a1[i]<<" ";cout<<a1[m1-1]<<endl;
}
cout<<m3<<" ";
for(int i=0;i<m3-1;i++)
cout<<a3[i]<<" ";cout<<a3[m3-1]<<endl;
cout<<m2<<" ";
for(int i=0;i<m2-1;i++)
cout<<a2[i]<<" ";cout<<a2[m2-1]<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: