【CodeForces】[300A]Array

Problem Description

Loading...

Problem Analysis

Still a Special Judge
so we just need to produce output that matches the intended answer
note that each group must contain at least 1 element
and a result is guaranteed to exist
so we can figure out the priority for assigning elements fairly easily

First put the 0s into the third group
put the values greater than 0 into the second group
and put the values less than 0 into the first group

Then first check whether the second group has any elements
if it does not
take two elements out of the first group and move them to the second group

Then check whether the number of elements in the first group is odd
if it is not, move one element to the third group

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<stdio.h>
#include<string.h>
int a[120],res[3][120];
int main() {
	int n;
	while(scanf("%d",&n)!=EOF) {
		memset(res,0,sizeof(0));
		for(int i=0; i<n; i++)
			scanf("%d",&a[i]);
		for(int i=0; i<n; i++) {
			if(a[i]==0) {
				res[2][0]++;
				res[2][res[2][0]]=a[i];
			} else if(a[i]>0) {
				res[1][0]++;
				res[1][res[1][0]]=a[i];
			} else {
				res[0][0]++;
				res[0][res[0][0]]=a[i];
			}
		}
		if(res[1][0]==0) {
			res[1][0]++;
			res[1][res[1][0]]=res[0][res[0][0]];
			res[0][0]--;
			res[1][0]++;
			res[1][res[1][0]]=res[0][res[0][0]];
			res[0][0]--;
		}
		if(res[0][0]%2==0) {
			res[2][0]++;
			res[2][res[2][0]]=res[0][res[0][0]];
			res[0][0]--;
		}
		for(int i=0; i<3; i++) {
			for(int j=0; j<=res[i][0]; j++) {
				printf("%d",res[i][j]);
				printf("%c",j==res[i][0]?'\n':' ');
			}
		}
	}
	return 0;
}

Problem link: 【CodeForces】[300A]Array

Loading...