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 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.

Examples

input

3-1 2 0

output

1 -11 21 0

input

4-1 -2 -3 0

output

1 -12 -3 -21 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;
}

  

时间: 2024-08-10 19:07:39

codeforces300A Array的相关文章

PHP Warning: array_multisort(): Array sizes are inconsistent

array_multisort() 函数返回排序数组.您可以输入一个或多个数组.函数先对第一个数组进行排序,接着是其他数组,如果两个或多个值相同,它将对下一个数组进行排序. 遇到这报错是两个数组对比不一致导致的, 如果是一维数组与二维数组进行排序可以用以下方法解决: 使用这个方法,会比较麻烦些,要将age提取出来存储到一维数组里,然后按照age升序排列.具体代码如下: 复制代码代码如下: $ages = array();foreach ($users as $user) {    $ages[]

详解go语言的array和slice 【二】

上一篇  详解go语言的array和slice [一]已经讲解过,array和slice的一些基本用法,使用array和slice时需要注意的地方,特别是slice需要注意的地方比较多.上一篇的最后讲解到创建新的slice时使用第三个索引来限制slice的容量,在操作新slice时,如果新slice的容量大于长度时,添加新元素依然后使源的相应元素改变.这一篇里我会讲解到如何避免这些问题,以及迭代.和做为方法参数方面的知识点. slice的长度和容量设置为同一个值 如果在创建新的slice时我们把

JavaScript的进阶之路(三)引用类型之Object类型和Array类型

引用类型 Object类型 function a(num){ if(num>3){ a(--num); } console.log(num); } a(5); //如何创建对象的实例 var obj1= new Object(); console.log(obj1); obj1.name="吴琼"; obj1.age=28; console.log(obj1.name+" "+obj1.age); //对象字面量语法 ,有点封装的感觉 var obj2 = {

LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,

实现一个函数clone,使JavaScript中的5种主要的数据类型(包括Number、String、Object、Array、Boolean)进行值复制

实现一个函数clone,可以对JavaScript中的5种主要的数据类型(包括Number.String.Object.Array.Boolean)进行值复制. 1 /** 对象克隆 2 * 支持基本数据类型及对象 3 * 递归方法 */ 4 function clone(obj) { 5 var o; 6 switch (typeof obj) { 7 case "undefined": 8 break; 9 case "string": o = obj + &q

421. Maximum XOR of Two Numbers in an Array

Given a non-empty array of numbers, a0, a1, a2, - , an-1, where 0 ≤ ai < 231. Find the maximum result of ai XOR aj, where 0 ≤ i, j < n. Could you do this in O(n) runtime? Example: Input: [3, 10, 5, 25, 2, 8] Output: 28 Explanation: The maximum resul

JavaScript------去掉Array中重复值

转载: http://blog.csdn.net/teresa502/article/details/7926796 代码: // 删除数组中重复数据 function removeDuplElem(array){ for(var i=0; i<array.length; i++){ for(var j=i+1; j<array.length;j++){ if(array[i]==array[j]){ array = removeElement(j,array);//删除指定下标的元素 i=-

Typescript : 遍历Array的方法:for, forEach, every等

方法一,for-of 这个貌似是最常用的方法,angular 2中HTML语法绑定也是要的这种语法. let someArray = [1, "string", false]; for (let entry of someArray) { console.log(entry); // 1, "string", false } for-in 官方文档上强调了for-in和for-of的区别: let list = [4, 5, 6]; for (let i in li

opendressinghash //use resize array

1 public class opendressinghash<Key, Value> { 2 private static final int INIT_CAPACITY = 4; 3 4 private int n; 5 private int m; 6 private Key[] keys; 7 private Value[] vals; 8 9 public opendressinghash() { 10 this(INIT_CAPACITY); 11 } 12 13 public o