问题描述
小蓝有 �N 个小球, 编号 1 至 �N 。其中 �−1N−1 是正品, 重量相同; 有 1 个是 次品, 重量比正品轻。
为了找出次品, 小蓝已经用天平进行了 �M 次称重, 并且记录下来每次两边 放的小球编号, 和称重结果。
请你根据记录, 判断还剩下几个小球有次品的嫌疑。
输入格式
第一行包含 2 个整数 �N 和 �M 。
以下包含 �M 次称重记录, 每个记录占 4 行。
第一行是一个整数 �K, 表示天平两边各放了 �K 个小球。
第二行包含 �K 个整数, 代表放在天平左边的小球编号。
第三行包含 �K 个整数, 代表放在天平右边的小球编号。
第四行是一个字符, 为 ' >> ', '<', ' == '之一。' >> ' 代表左边比右边重, '<' 代
在一次称重中保证每个小球最多出现 1 次。
输出格式
输出一个整数, 代表答案。
样例输入
10 2
3
1 2 3
4 5 6
<
2
3 7
8 9
=
样例输出
2
样例说明
1,2,3<4,5,61,2,3<4,5,6 能判断出次品在 1,2,31,2,3 之中。
3,7=8,93,7=8,9 能判断出 3 不可能是次品。
所以只剩下 1,21,2 可能是次品。
评测用例规模与约定
对于 40%40% 的数据, 1≤�≤1061≤N≤106;
对于 100%100% 的数据, 1≤�≤109,1≤�≤1051≤N≤109,1≤M≤105, 参与 �M 次称重的小球总数 ≤106≤106
思路:使用Map记录出现次数最多的嫌疑球后面遍历判断就好啦
import java.io.*;
import java.math.BigInteger;
import java.util.*;
/*
10 2
5
1 2 3 4 5
6 7 8 9 10
<
4
6 8 9 10
4 3 2 1
>
*/
public class Main {
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) {
Input sc = new Input(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
Map<Integer, Integer> map = new HashMap<>();
while (m-- > 0) {
int t = sc.nextInt();
int[] a = new int[t + 1];
for (int i = 1; i <= t; i++) a[i] = sc.nextInt();
int[] b = new int[t + 1];
for (int i = 1; i <= t; i++) b[i] = sc.nextInt();
char c = sc.next().charAt(0);
//map.merge 如果存在key key对应的value加1否则为1
if (c == '<') {
for (int i = 1; i <= t; i++) {
map.merge(a[i], 1, Integer::sum);
}
} else if (c == '>') {
for (int i = 1; i <= t; i++) {
map.merge(b[i], 1, Integer::sum);
}
} else {
for (int i = 1; i <= t; i++) {
map.put(a[i], -99);
map.put(b[i], -99);
}
}
}
int max = 0;
//寻找嫌疑次数出现最多的球
for (int x : map.values()) {
max = Math.max(max, x);
}
//全部相等的
if (max == 0) {
System.out.println(n - map.size());
return;
}
int ans = 0;
for (int x : map.values()) {
if (x == max) {
ans++;
}
}
out.println(ans);
out.flush();
out.close();
}
//以下是快读部分不必理会
static class Input {
public BufferedReader reader;
public StringTokenizer tokenizer;
public Input(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
String str = null;
try {
str = reader.readLine();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return str;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public Double nextDouble() {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
}
}