题解 | 大整数哈希

大整数哈希

https://www.nowcoder.com/practice/29f0cff8a69b4ab6a2f63fb7386defa3

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        // 消耗空行
        in.nextLine();
        // 大小特别大不能创造这么大的数组,我们就维护一个hash表,若这个数存在就代表已经改过数据
        // 若没有改过就代表为0
        long res = 0L;
        Map<Long,Long> maps = new HashMap<>();
        for(int i = 1 ; i <= n ; i++){
            String[] parts=in.nextLine().split(" ");
            long x = Long.parseUnsignedLong(parts[0]);
            long y = Long.parseUnsignedLong(parts[1]);
            long ans=maps.getOrDefault(x,0L);
            maps.put(x,y);
            res = res + i*ans;
        }
        System.out.println(Long.toUnsignedString(res));
    }
}

全部评论

相关推荐

牛客41406533...:回答他在课上学,一辈子待在学校的老教授用三十年前的祖传PPT一字一句的讲解,使用谭浩强红皮书作为教材在devc++里面敲出a+++++a的瞬间爆出114514个编译错误来学这样才显得专业
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务