您的位置:首页 > 其它

CodeForces 300A Array

2016-06-06 18:24 639 查看
CodeForces
300A Array
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d
& %I64u

Submit Status

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

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


AC代码:

#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int n;
int a[110];
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1);
int k;
for(int i=1;i<=n;i++)
if(a[i]==0)
{
k=i;
break;
}
if((k-1)%2!=0)
{
printf("1 %d\n",a[k-1]);
printf("%d",n-2);
for(int i
4000
=1;i<=k-2;i++)
printf(" %d",a[i]);
if(k!=n)
{
for(int j=k+1;j<n;j++)
printf(" %d",a[j]);
printf(" %d\n",a
);
}
else
printf("\n");
printf("1 0\n");
}
else
{
printf("1 %d\n",a[k-1]);
printf("%d",n-3);
for(int i=1;i<=k-3;i++)
printf(" %d",a[i]);
if(k!=n)
{
for(int j=k+1;j<n;j++)
printf(" %d",a[j]);
printf(" %d\n",a
);
}
else
printf("\n");
printf("2 0 %d\n",a[k-2]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: