第一次
one 使用map 内存超限(60)
two 纯循环 运行超时(60)
three 字符串 运行超时(30)
续:错误的原因在于 :开辟了n长度的数组,所以无论怎么改都不会有改进。
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class csp_20_06_02 {
public static void main(String[] args){
//10 3 4
//4 5
//7 -3
//10 1
//1 10
//4 20
//5 30
//7 40
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int a = input.nextInt();
int b = input.nextInt();
int[] x = new int[n];
int[] y = new int[n];
long result=0;
for(int i =0;i<a;i++){
int temp = input.nextInt();
x[temp-1] = input.nextInt();
}
for(int i =0;i<b;i++){
int temp1 = input.nextInt();
int temp2 = input.nextInt();
result += x[temp1-1]*temp2 ;
}
System.out.print(result);
//one(); //方法一
//two(); //方法二
//three(): //方法三
}
public void one(){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int a = input.nextInt();
int b = input.nextInt();
int result=0;
Map<Integer,Integer> map_u = new HashMap();
Map<Integer,Integer> map_v = new HashMap();
//获取输入
for (int i = 0; i < a; i++) {
int temp1 = input.nextInt();
int temp2 = input.nextInt();
map_u.put(temp1,temp2);
}
for (int i = 0; i < b; i++) {
int temp1 = input.nextInt();
int temp2 = input.nextInt();
map_v.put(temp1,temp2);
}
for(Map.Entry<Integer,Integer>temp :map_u.entrySet()){
int t = temp.getKey();
if(map_v.containsKey(t)){
result += map_u.get(t)*map_v.get(t);
}
}
System.out.print(result);
}
public void two(){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int a = input.nextInt();
int b = input.nextInt();
int[] x = new int[n];
int[] y = new int[n];
int result=0;
for(int i =0;i<a;i++){
int temp = input.nextInt();
x[temp-1] = input.nextInt();
}
for(int i =0;i<b;i++){
int temp = input.nextInt();
y[temp-1] = input.nextInt();
}
//或者
// for(int i =0;i<b;i++){
// int temp1 = input.nextInt();
// int temp2 = input.nextInt();
// result += x[temp1-1]*temp2 ;
// }
for(int i =0;i<n;i++){
result += x[i]*y[i];
}
System.out.print(result);
}
public void three(){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int a = input.nextInt();
int b = input.nextInt();
String sa="";
int[] x = new int[n];
int[] y = new int[n];
long result=0;
for(int i =0;i<a;i++){
int temp = input.nextInt();
x[temp-1] = input.nextInt();
sa+=temp+" ";
}
for(int i =0;i<b;i++){
int temp1 = input.nextInt();
int temp2 = input.nextInt();
if(sa.contains(temp1+""))
result += x[temp1-1]*temp2 ;
}
System.out.print(result);
}
}
第二次(正确)
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int a = input.nextInt();
int b = input.nextInt();
int[][] x = new int[a][2];
long result=0;
for(int i =0;i<a;i++){
x[i][0] = input.nextInt();
x[i][1] = input.nextInt();
}
int index = 0;
for(int i =0;i<b;i++){
int index_y = input.nextInt();
int value_y = input.nextInt();
if(index<a && x[index][0] == index_y){
result += x[index][1] * value_y;
}else{
while(index<a && x[index][0] < index_y)
index++;
if(index<a && x[index][0] == index_y){
result += x[index][1] * value_y;
}
}
}
System.out.print(result);
}
}