AcWing 3288. 稀疏向量-java-简单易懂-hashmap
原题链接
简单
作者:
西红柿炒番茄_7
,
2024-12-04 20:54:45
,
所有人可见
,
阅读 2
Java 代码
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a = sc.nextInt();
int b = sc.nextInt();
HashMap<Integer, Pair> map = new HashMap<>();
while (a-->0) {
int index = sc.nextInt();
int value = sc.nextInt();
map.put(index, new Pair(value,0));
}
while (b-->0) {
int index = sc.nextInt();
int value = sc.nextInt();
if (map.containsKey(index)) {
Pair pair = map.get(index);
pair.v2 = value;
}
}
Set<Map.Entry<Integer, Pair>> entries = map.entrySet();
long res = 0;
for (Map.Entry<Integer, Pair> entry : entries) {
Pair pair = entry.getValue();
res += (long) pair.v1 * (long) pair.v2;
}
System.out.println(res);
}
static class Pair {
int v1;
int v2;
Pair (int v1, int v2) {
this.v1 = v1;
this.v2 = v2;
}
}
}