CodeForces 300A Array(水题)

来源:互联网 发布:汽车售后服务网络建设 编辑:程序博客网 时间:2024/04/28 10:20
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:

  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.

Sample test(s)
input
3-1 2 0
output
1 -11 21 0
input
4-1 -2 -3 0
output
1 -12 -3 -2

1 0

是道水题,前提你得看懂题目,我最开始是没有看懂题目的,都不知道是什么意思,虽然猜想与最终的要求一致,但是不敢下手,题目是,给你个序列,让你分为三组数据,第一组,所有数相乘小于零,第二组,相乘大于零,第三组数据等于零,第三组数据直接可以看出,其中一定会有一个零,且任何数加在这组数据中都满足条件

C++代码,是比赛的时候做的,后来发现自己发神经,大家可以简写,非常之水

#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <queue>#include <set>using namespace std;typedef long long LL;const int MAXN = 1e2 + 5;int Ar[MAXN];int n;vector<int>G[4];int main() {    scanf("%d", &n);    int cnt = 0,x;    for(int i = 0 ; i < n; i ++) {        scanf("%d", &x);        Ar[cnt ++] = x;    }    sort(Ar, Ar + cnt);    int len = lower_bound(Ar,Ar + cnt,0) - Ar;    bool flags = true;    if(len == cnt - 1) flags = false;    len --;    if(len == 0) {        G[1].push_back(Ar[0]);        for(int i = 1; i < cnt; i ++) {            if(Ar[i] != 0)                G[2].push_back(Ar[i]);        }        G[3].push_back(0);    } else {        int flag = -1;        if(len & 1) {            G[3].push_back(Ar[len]);            flag = len;            len --;        }        if(len == 0) {            G[1].push_back(Ar[0]);            for(int i = 1; i < cnt; i ++) {                if(Ar[i] != 0 && flag != i)                    G[2].push_back(Ar[i]);            }            G[3].push_back(0);        } else {            if(!flags) len = 0;            for(int i = 0; i < cnt; i ++) {                if(i <= len)G[1].push_back(Ar[i]);                else if(Ar[i] == 0) G[3].push_back(Ar[i]);                else if(flag != i && Ar[i] != 0) G[2].push_back(Ar[i]);            }        }    }    for(int i = 1; i <= 3; i ++) {        printf("%d",G[i].size());        for(int j = 0; j < G[i].size(); j ++) {            printf(" %d",G[i][j]);        }        printf("\n");    }    return 0;}


提供另外的代码:

n = int(input())L = list(map(int,input().split()))L1 = []L2 = []a = 0b = 0for x in L:    if x > 0:        a = a + 1    elif x < 0:        b = b + 1L.sort()if a > 0:    for i in L:        if i == L[-1]:            print(1,i)            break        elif i < 0 and i == L[0]:            print(1,i)        else:            L1.append(i)    print(len(L1),end = ' ')    for i in L1:        if i != L1[-1]:            s = ' '        else :            s = '\n'        print(i,end = s)else:    for i in L:        if i < 0 and i == L[0]:            print(1,i)        elif i < 0 and i <= L[2]:            L2.append(i)            if i == L[2]:                print(len(L2),end = ' ')                for j in L2:                    if j == L2[-1]:                        s = '\n'                    else :                        s = ' '                    print(j,end = s)        else:            L1.append(i)    print(len(L1),end = ' '),    for i in L1:        if i != L1[-1]:            s = ' '        else :            s = '\n'        print(i,end = s)



0 0