题目链接:
Problem - A - Codeforces
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:
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| ≤ 10^3) — 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 1
Input | Output |
---|---|
3 -1 2 0 | 1 -1 1 2 1 0 |
Sample 2
Input | Output |
---|---|
4 -1 -2 -3 0 | 1 -1 2 -3 -2 1 0 |
思路:
思维题,分别用三个vector数组存储正数,负数和小数,然后分情况讨论.
参考代码:
#include
using namespace std;inline int read()
{bool sym = 0;int res = 0;char ch = getchar();while (!isdigit(ch)) sym |= (ch == '-'), ch = getchar();while (isdigit(ch)) res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();return sym ? -res : res;
}int main()
{int n=read();vector positive,negative,zero;while(n--){int x=read();if(!x)zero.push_back(x);else if(x>0)positive.push_back(x);else if(x<0)negative.push_back(x);}if((negative.size()&1) && (!positive.size())){printf("%d ",negative.size()-2);for(unsigned int i=2;i