codeforces300A Array

Posted 非我非非我

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codeforces300A 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 n(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 n(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 n(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.

Examples
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

 题意:给一组数,要求将其分为三组 每组数乘积分别<0  ==0   >0  输出每组数的个数及这些数(数据满足至少一组解)

题解第一组直接输出一个负数,第二组输出一个正数或两个负数  第三组输出剩余的全部

#include<stdio.h>
#include<string.h>
#include<cstdio> 
#include<string>
#include<math.h>
#include<algorithm>
#define LL long long
#define PI atan(1.0)*4
#define DD double
#define MAX 300
#define mod 100
#define dian 1.000000011
#define INF 0x3f3f3f
using namespace std;
int s[MAX],k[MAX];
int main()
{
	int n,m,j,i,t,a,b,c;
	while(scanf("%d",&n)!=EOF)
	{
		for(i=0;i<n;i++)
		    scanf("%d",&s[i]);
		int flag=0;
		for(i=0;i<n;i++)
		{
			if(s[i]>0)
			{
				b=i;
				flag=1;
			}
		}
		for(i=0;i<n;i++)
	    {
	    	if(s[i]<0)
	    	{
	    		c=i;
	    		break;
	    	}
	    }
	    j=0;
	    if(!flag)
	    for(i=0;i<n;i++)
	    {
	    	if(i!=c)
	    	{
	    		if(s[i]<0)
	    		    k[j++]=s[i];
	    	}
	    }
	    printf("1 %d\n",s[c]);
	    if(flag)
	    	printf("1 %d\n",s[b]);
	    else
	    	printf("2 %d %d\n",k[0],k[1]);
	    if(flag)
	    {
	    	printf("%d ",n-2);
	    	for(i=0;i<n;i++)
	    	{
	    		if(s[i]!=s[c]&&s[i]!=s[b])
	    		printf("%d ",s[i]);
	    	}
	    	printf("\n");
	    }
	    else
	    {
	    	printf("%d ",n-3);
	    	for(i=0;i<n;i++)
	    	{
	    		if(s[i]!=s[c]&&s[i]!=k[0]&&s[i]!=k[1])
	    		printf("%d ",s[i]);
	    	}
	    	printf("\n");
	    }
	}
	return 0;
} 

  

以上是关于codeforces300A Array的主要内容,如果未能解决你的问题,请参考以下文章

js代码片段: utils/lcoalStorage/cookie

[Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)][C. Playing Piano](代码片段

[TIA PORTAL][CONVERT] Convert Char Array to DInt...DInt to Char Array..Useful and easy function(代码片段

Discuz代码片段

c_cpp Codeforces片段

javascript常用代码片段