20-06-02(稀疏向量)

第一次

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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值