N - N
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit Status
Description
Vitalyhas an array of n distinctintegers. Vitaly wants to divide this array into three non-empty setsso as the following conditions hold:
1. The product of all numbers in thefirst set is less than zero ( < 0).
2. The product of all numbers in thesecond set is greater than zero ( > 0).
3. The product of all numbers in thethird set is equal to zero.
4. Each number from the initial arraymust occur in exactly one set.
HelpVitaly. Divide the given array.
Input
Thefirst line of the input contains integer n(3 ≤ n ≤ 100).The second line contains n space-separateddistinct integersa1, a2, ..., an(|ai| ≤ 103) —the array elements.
Output
Inthe 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.
Inthe 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.
Inthe 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.
Theprinted sets must meet the described conditions. It is guaranteed that thesolution exists. If there are several solutions, you are allowed to print anyof them.
Sample Input
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
看懂题意很容易。
思路:(繁琐的思路,附有繁琐代码)分交叉的情况讨论,三种情况就是:负数个数为奇数或者偶数,有无正数。。。。
清晰的思路,慢慢思考就是,输出排序后的第一个数,最后讨论数组尾数的取值,>0就输出尾数的值,最后把整个数组的其他元素给 0 的取值数组;否则,输出最前的两个负数。
代码:
/*=============================AC情况===============================*/
/*题目网址: */
/*时间: */
/*心得: */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#define G 113
using namespace std;
int main() {
int a[G],n1[G],n2[G],n3[G];
int n,fu,zh,li;
while(scanf("%d",&n)!=EOF) {
for(int j=0; j<n; j++)
scanf("%d",&a[j]);
sort(a,a+n);
printf("1 %d\n",a[0]);
if(a[n-1]>0) {
printf("1 %d\n",a[n-1]);
printf("%d ",n-2);
for(int j=1; j<n-2; j++)
printf("%d ",a[j]);
printf("%d\n",a[n-2]);
} else {
printf("2 %d %d\n",a[1],a[2]);
printf("%d ",n-3);
for(int j=3; j<n-1; j++)
printf("%d ",a[j]);
printf("%d\n",a[n-1]);
}
}
return 0;
}
/*********************************测试数据*********************************
**************************************************************************/
繁琐代码(不完整):
/*=============================AC情况===============================*/
/*题目网址: */
/*时间: */
/*心得: */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#define G 113
using namespace std;
int main() {
int a[G],n1[G],n2[G],n3[G];
int n,fu,zh,li;
while(scanf("%d",&n)!=EOF) {
for(int j=0; j<n; j++)
scanf("%d",&a[j]);
fu=zh=li=0;
sort(a,a+n);
for(int j=0; j<n; j++) {
if(a[j]<0) {
n1[fu]=a[j];
fu++;
} else if(a[j]>0) {
n2[zh]=a[j];
zh++;
} else {
n3[li]=a[j];
li++;
}// printf("%d %d %d",fu,zh,li);
}
if(zh==0) {
if(fu%2==0) { //负数个数为偶数
zh=2;
printf("%d ",fu-3);
for(int j=2; j<fu-1; j++)
printf("%d ",fu[j]);
printf("\n");
printf("%d ",zh);
for(int j=0; j<2; j++)
printf("%d "fu[j]);
printf("\n");
printf("%d ",li+1);
for(int j=0; j<li; j++)
printf("%d "li[j]);
printf("%d\n",a3[fu-1]);
} else {
zh=2;
printf("%d ",fu-2);
for(int j=2; j<fu; j++)
printf("%d ",fu[j]);
printf("\n");
printf("%d ",zh);
for(int j=0; j<2; j++)
printf("%d "fu[j]);
printf("\n");
printf("%d ",li);
for(int j=0; j<li; j++)
printf("%d "li[j]);
printf("\n");
}
} else {
if(fu%2==0) { //负数个数为偶数
printf("%d ",fu-1);
for(int j=0; j<fu-1; j++)
printf("%d ",n1[j]);
printf("\n");
printf("%d ",zh);
for(int j=0; j<zh; j++)
printf("%d ",n2[j]);
printf("\n");
printf("%d %d ",a1[fu-1],li+1);
for(int j=0; j<li; j++)
printf("%d "li[j]);
printf("\n");
} //else {
//}
}
}
return 0;
}
/*********************************测试数据*********************************
**************************************************************************/