输入输出样例

样例1

输入

  1. 10 3 4
  2. 4 5
  3. 7 -3
  4. 10 1
  5. 1 10
  6. 4 20
  7. 5 30
  8. 7 40

输出

  1. -20

题解一

60分。超时。

  1. import java.util.*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner scanner = new Scanner(System.in);
  5. int n = scanner.nextInt();
  6. int a = scanner.nextInt();
  7. int b = scanner.nextInt();
  8. Map<Integer, Integer> map = new HashMap<>();
  9. for (int i = 0, key, value; i < a; ++i) {
  10. key = scanner.nextInt();
  11. value = scanner.nextInt();
  12. map.put(key, value);
  13. }
  14. long ans = 0;
  15. for (int i = 0, key, value; i < b; ++i) {
  16. key = scanner.nextInt();
  17. value = scanner.nextInt();
  18. if (map.containsKey(key)) {
  19. ans += map.get(key) * value;
  20. }
  21. }
  22. System.out.println(ans);
  23. }
  24. }

题解二

60分。超时。

  1. import java.util.*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner scanner = new Scanner(System.in);
  5. int n = scanner.nextInt();
  6. int a = scanner.nextInt();
  7. int b = scanner.nextInt();
  8. Pair[] arrA = new Pair[a];
  9. Pair[] arrB = new Pair[b];
  10. for (int i = 0; i < a; ++i) {
  11. arrA[i] = new Pair(scanner.nextInt(), scanner.nextInt());
  12. }
  13. for (int i = 0; i < b; ++i) {
  14. arrB[i] = new Pair(scanner.nextInt(), scanner.nextInt());
  15. }
  16. long ans = 0;
  17. int ptrA = 0;
  18. int ptrB = 0;
  19. while ((ptrA < arrA.length) && (ptrB < arrB.length)) {
  20. if (arrA[ptrA].key == arrB[ptrB].key) {
  21. ans += arrA[ptrA].value * arrB[ptrB].value;
  22. ++ptrA;
  23. ++ptrB;
  24. } else if (arrA[ptrA].key > arrB[ptrB].key) {
  25. ++ptrB;
  26. } else {
  27. ++ptrA;
  28. }
  29. }
  30. System.out.println(ans);
  31. }
  32. }
  33. class Pair {
  34. int key;
  35. int value;
  36. public Pair(int key, int value) {
  37. super();
  38. this.key = key;
  39. this.value = value;
  40. }
  41. }

题解三

居然是因为读入效率的问题。。。换成BufferedReader后满分通过。
参考链接:https://blog.csdn.net/tylz970408/article/details/108205028?utm_medium=distribute.pc_relevant.none-task-blog-blogcommendfrommachinelearnpai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-blogcommendfrommachinelearnpai2-2.channel_param

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5. public class Main {
  6. public static void main(String[] args) throws IOException {
  7. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  8. String[] strs = reader.readLine().trim().split(" ");
  9. int n = Integer.parseInt(strs[0]);
  10. int a = Integer.parseInt(strs[1]);
  11. int b = Integer.parseInt(strs[2]);
  12. Pair[] arrA = new Pair[a];
  13. Pair[] arrB = new Pair[b];
  14. String[] s;
  15. for (int i = 0; i < a; ++i) {
  16. s = reader.readLine().trim().split(" ");
  17. arrA[i] = new Pair(Integer.parseInt(s[0]), Integer.parseInt(s[1]));
  18. }
  19. for (int i = 0; i < b; ++i) {
  20. s = reader.readLine().trim().split(" ");
  21. arrB[i] = new Pair(Integer.parseInt(s[0]), Integer.parseInt(s[1]));
  22. }
  23. long ans = 0;
  24. int ptrA = 0;
  25. int ptrB = 0;
  26. while ((ptrA < arrA.length) && (ptrB < arrB.length)) {
  27. if (arrA[ptrA].key == arrB[ptrB].key) {
  28. ans += arrA[ptrA].value * arrB[ptrB].value;
  29. ++ptrA;
  30. ++ptrB;
  31. } else if (arrA[ptrA].key > arrB[ptrB].key) {
  32. ++ptrB;
  33. } else {
  34. ++ptrA;
  35. }
  36. }
  37. System.out.println(ans);
  38. }
  39. }
  40. class Pair {
  41. int key;
  42. int value;
  43. public Pair(int key, int value) {
  44. super();
  45. this.key = key;
  46. this.value = value;
  47. }
  48. }