git reimport
This commit is contained in:
58
archive/2019.02/2019.02.27 - Hello 2019/TaskC.java
Normal file
58
archive/2019.02/2019.02.27 - Hello 2019/TaskC.java
Normal file
@@ -0,0 +1,58 @@
|
||||
package chelper;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import io.InputReader;
|
||||
import io.OutputWriter;
|
||||
import solutions.ChelperSolution;
|
||||
|
||||
|
||||
public class TaskC extends ChelperSolution {
|
||||
|
||||
@Override
|
||||
public void solve(int testNumber, InputReader in, OutputWriter out) {
|
||||
super.solve(testNumber, in, out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void solve(int testNumber) {
|
||||
int n = in.nextInt();
|
||||
|
||||
int equals = 0;
|
||||
Map<Integer, Integer> counts = new HashMap<>();
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
String s = in.nextString();
|
||||
|
||||
int balance = 0;
|
||||
int min = 0;
|
||||
for (char c : s.toCharArray()) {
|
||||
if (c == '(') {
|
||||
balance++;
|
||||
} else {
|
||||
balance--;
|
||||
}
|
||||
min = Math.min(min, balance);
|
||||
}
|
||||
|
||||
if (min == 0 && balance == 0) {
|
||||
equals++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((min == 0 && balance > 0) || (min < 0 && balance == min)) {
|
||||
counts.put(balance, 1 + counts.getOrDefault(balance, 0));
|
||||
}
|
||||
}
|
||||
|
||||
int res = equals / 2;
|
||||
for (Map.Entry<Integer, Integer> entry : counts.entrySet()) {
|
||||
if (entry.getKey() > 0) {
|
||||
res += Math.min(entry.getValue(), counts.getOrDefault(-entry.getKey(), 0));
|
||||
}
|
||||
}
|
||||
|
||||
out.println(res);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user