-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDevuArrayPartition.java
152 lines (131 loc) · 4.18 KB
/
DevuArrayPartition.java
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* Created on 6/4/14
*
* @author: Anirudh Rayabharam <[email protected]>
* @handle: code_overlord
*/
import java.io.*;
import java.util.Vector;
public class DevuArrayPartition {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintStream out = System.out;
int n = in.nextInt();
int k = in.nextInt();
int p = in.nextInt();
Vector<Integer> evens = new Vector<Integer>();
Vector<Integer> odds = new Vector<Integer>();
int oddCount = 0, evenCount = 0;
for (int i = 0; i < n; i++) {
int x = in.nextInt();
if (x % 2 == 0) {
evenCount++;
evens.add(x);
}
else {
oddCount++;
odds.add(x);
}
}
if (oddCount < k-p || (oddCount - k + p) % 2 == 1) {
out.println("NO");
return;
}
int oddsReqForEven = 0;
if (evenCount < p) {
if (oddCount-k+p < 2*(p-evenCount) || (oddCount-k+p-2*(p-evenCount)) % 2 == 1) {
out.println("NO");
return;
}
oddsReqForEven = oddCount-k+p;
}
out.println("YES");
int el = evens.size();
int ol = odds.size();
int ei = 0, oi = 0;
// print odds
for (int i = 1; i <= k-p; i++) {
if (i == k-p && oddsReqForEven == 0) {
if (p == 0)
out.print((ol-oi+el) + " ");
else
out.print((ol-oi) + " ");
while(oi < ol) {
out.print(odds.get(oi++));
if (oi < ol) out.print(" ");
}
if (p == 0) {
out.print(" ");
while(ei < el) {
out.print(evens.get(ei++));
if (ei < el) out.print(" ");
}
}
out.println();
break;
}
out.println("1 " + odds.get(oi++));
}
// print evens
for (int i = 1; i <= p; i++) {
if (i == p && ei == el) {
out.print((ol-oi) + " ");
while(oi < ol) {
out.print(odds.get(oi++) + " ");
}
out.println();
break;
} else if (i == p) {
if (k-p == 0)
out.print((el-ei+ol) + " ");
else
out.print((el-ei) + " ");
while(ei < el) {
out.print(evens.get(ei++));
if (ei < el) out.print(" ");
}
if (k-p==0) {
out.print(" ");
while (oi < ol) {
out.print(odds.get(oi++));
if (oi < ol) out.print(" ");
}
}
out.println();
break;
}
if (ei == el) {
out.print("2 ");
out.print(odds.get(oi++) + " ");
out.println(odds.get(oi++));
} else
out.println("1 " + evens.get(ei++));
}
}
private static class InputReader {
public BufferedReader reader;
private int tokenCount, nextTokenIndex;
private String[] tokens;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenCount = nextTokenIndex = 0;
}
public String next() {
String nextLine;
if (nextTokenIndex == tokenCount) {
try {
nextLine = reader.readLine();
nextTokenIndex = 0;
tokens = nextLine.split("\\s");
tokenCount = tokens.length;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokens[nextTokenIndex++];
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}