acm Array

论坛 期权论坛 脚本     
匿名技术用户   2020-12-28 23:10   11   0

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:

  1. The product of all numbers in the first set is less than zero (<0).
  2. The product of all numbers in the second set is greater than zero (>0).
  3. The product of all numbers in the third set is equal to zero.
  4. 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.

Example
Input
3
-1 2 0
Output
1 -1
1 2
1 0
Input
4
-1 -2 -3 0
Output
1 -1
2 -3 -2
思考:一定存在解决方案,则必有0和至少一个负数且不同整数
//最简单的,第一列只有一个负数,第二列最多两个,其余归到第三列
//可以没有正数,必须有负数
//= =哪里用栈和队列啊?

#include<stdio.h>
#include<algorithm>
using namespace std;
bool cmp(int a,int b)
{
 return a>b;
}
int a[100],b[100],c[100];
int main()
{
 int n,i,m[100];
 while(scanf("%d",&n)!=EOF)
 {
  int l=0,s=0;
  for(i=0;i<n;i++)
  {
   scanf("%d",&m[i]);
   if(m[i]<0)
   {
    a[i]=m[i];
    l++;
   }
   
   else 
     if(m[i]>0)
       {
       b[i]=m[i]; 
       s++;
    }
  }
  sort(a,a+n);
  sort(b,b+n,cmp);
  int q=0,w=0;
  
  if(l>2)
   q=s+2;
  else//l=1,2 
      q=s;
   w=1;
   printf("%d ",1);
     printf("%d\n",a[0]);
 
    if(l>2)
    {
     printf("%d ",q);
     for(i=1;i<3;i++)
          printf("%d ",a[i]);
      if(s>0)
      {
       for(i=0;i<s-1;i++)
     printf("%d ",b[i]);
     printf("%d",b[s-1]);
   }
        printf("\n");
   printf("%d ",n-q-w);
    for(i=3;i<l;i++)
     printf("%d ",a[i]);
     printf("0\n"); 
     
    }
  else//l<2
    {
      printf("%d ",q);
   for(i=0;i<s-1;i++)
     printf("%d ",b[i]);
     printf("%d\n",b[s-1]);
      printf("%d ",n-q-w);
      if(l==2) 
     printf("%d ",a[1]);
     printf("0\n");
  }
  
 }
 
 return 0;

}


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP