暑期集训之Array

来源:互联网 发布:vue.js 路由 编辑:程序博客网 时间:2024/06/06 16:25

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 -11 21 0
Input
4-1 -2 -3 0
Output
1 -12 -3 -21 0

这道题说实话,虽然是在栈和队列的专题里面,但就像0和5那道题一样,用sort就解决了,或许有用到栈或者队列做,但我没想到怎么做,所以还是按着简单的方法写了。

其实,说白了这道题就是分情况的题,题目说了,如果结果不唯一,那么输出一种情况就好了,所以根据这一情况,我们可以输出最简单的那种情况,不过我还是先把这道题要分的情况都写出来把:

1.负数的个数为奇数,不存在偶数

2.负数的个数为奇数,存在偶数

3.负数的个数为偶数,不存在偶数

4.负数的个数为偶数,存在偶数

由题意可知,我们最后输出的是相乘小于零,大于零,和等于零这三种情况,所以偶数数量是奇数还是偶数对结果没有影响,所以只考虑负数的个数就好,其实说到这里应该也就知道是啥意思了,也就不多说啥了,具体代码如下,很简单,唯一的难点就是情况有点多,只要分好情况,再一一讨论就可以AC了,话不多说,看代码吧:

#include<stdio.h>#include<string.h>#include<iostream>#include<vector>#include<stack>#include<queue>#include<algorithm>using namespace std;int main(){int n;int a[105];while(scanf("%d",&n)!=EOF){   int zheng=0,fu=0,shu=n;for(int i=0;i<n;i++)scanf("%d",&a[i]);sort(a,a+n); for(int i=0;i<n;i++){if(a[i]<0)fu++;else if(a[i]>0)zheng++;}    if(fu==1)    {    printf("1 %d\n",a[0]);    printf("%d",n-2);    for(int i=1;zheng>0;i++){    if(a[i]!=0){printf(" %d",a[i]);zheng--;}if(zheng==0)printf("\n");}printf("1 0\n");}else if((fu-1)%2==0){   printf("1 %d\n",a[0]);    printf("%d",n-2);for(int i=1;i<n;i++){if(a[i]==0)continue;else{printf(" %d",a[i]);    shu--;}if(shu==2)printf("\n");}printf("1 0\n");}else if(zheng==0&&(fu-1)%2!=0){   printf("1 %d\n",a[0]);     printf("%d",n-3);for(int i=1;i<n-2;i++)printf(" %d",a[i]);printf("\n");printf("2 %d 0\n",a[n-2]);}else if(zheng!=0&&(fu-1)%2!=0){printf("1 %d\n",a[0]);     printf("%d",n-3);for(int i=2;i<n;i++){if(a[i]==0)continue;else{printf(" %d",a[i]);shu--;}if(shu==3)printf("\n");}printf("2 %d 0\n",a[1]);}}return 0;}




原创粉丝点击