-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathG_Array.cpp
80 lines (73 loc) · 1.67 KB
/
G_Array.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stack>
#include <map>
#include <iostream>
#include <vector>
using namespace std;
void printResult(vector<int> zeros, vector<int> positives, vector<int> negatives)
{
cout << negatives.size() << " ";
for (int i = 0; i < negatives.size(); i++)
{
cout << negatives[i] << " ";
}
cout << endl;
cout << positives.size() << " ";
for (int i = 0; i < positives.size(); i++)
{
cout << positives[i] << " ";
}
cout << endl;
cout << zeros.size() << " ";
for (int i = 0; i < zeros.size(); i++)
{
cout << zeros[i] << " ";
}
cout << endl;
}
int main()
{
vector<int> positives;
vector<int> negatives;
vector<int> zeros;
int negativesCount = 0;
int n;
cin >> n;
while (n--)
{
int x;
cin >> x;
if (x == 0)
{
zeros.push_back(x);
}
else if (x < 0)
{
negatives.push_back(x);
}
else if (x > 0)
{
positives.push_back(x);
}
}
if (negatives.size() % 2 != 0)
{
while (negatives.size() > 1)
{
positives.push_back(negatives.back());
negatives.pop_back();
}
}
else
{
zeros.push_back(negatives.back());
negatives.pop_back();
}
// Positives might be empty so adding a pair of negative elements to it will it non-empty to match the problem's constraints.
if(positives.empty()) {
positives.push_back(negatives.back());
negatives.pop_back();
positives.push_back(negatives.back());
negatives.pop_back();
}
printResult(zeros, positives, negatives);
}