CodeForces 300A Array(水题)

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 -1
1 2
1 0
input
4
-1 -2 -3 0
output
1 -1
2 -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 = 0
b = 0
for x in L:
    if x > 0:
        a = a + 1
    elif x < 0:
        b = b + 1
L.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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值