题目要求

P5143题目链接

分析

一个排序求解的题,C++很容易AC,但Java党需要做一些性能优化,下面做一下分析和总结。

第一版代码(80分):

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;public class Main {private static class Hill {int x;int y;int z;public Hill(int x, int y, int z) {this.x = x;this.y = y;this.z = z;}}public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int num = scanner.nextInt();Hill[] hills = new Hill[num];for (int i = 0; i < num; i++) {hills[i] = new Hill(scanner.nextInt(), scanner.nextInt(), scanner.nextInt());}Arrays.sort(hills, Comparator.comparing(e -> e.z));scanner.close();int x = hills[0].x, y = hills[0].y, z = hills[0].z;double sum = 0;for (int i = 1; i < num; i++) {sum += Math.sqrt(Math.pow(hills[i].x-x, 2) + Math.pow(hills[i].y-y, 2) + Math.pow(hills[i].z-z, 2));x = hills[i].x;y = hills[i].y;z = hills[i].z;}System.out.printf("%.3f", sum);}}

第二版代码(80分):

将Scanner换成了BufferedReader,个别点有所优化,整体没有突破性能瓶颈。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;public class Main {private static class Hill {int x;int y;int z;public Hill(int x, int y, int z) {this.x = x;this.y = y;this.z = z;}}public static void main(String[] args) throws IOException {BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));int num = Integer.parseInt(reader.readLine());Hill[] hills = new Hill[num];for (int i = 0; i < num; i++) {String[] temp = reader.readLine().split("\\s+");hills[i] = new Hill(Integer.parseInt(temp[0]), Integer.parseInt(temp[1]), Integer.parseInt(temp[2]));}Arrays.sort(hills, Comparator.comparing(e -> e.z));reader.close();int x = hills[0].x, y = hills[0].y, z = hills[0].z;double sum = 0;for (int i = 1; i < num; i++) {sum += Math.sqrt(Math.pow(hills[i].x-x, 2) + Math.pow(hills[i].y-y, 2) + Math.pow(hills[i].z-z, 2));x = hills[i].x;y = hills[i].y;z = hills[i].z;}System.out.printf("%.3f", sum);}}

第三版代码(80分):

使用short替换int,赌测试数据不会爆short,结果真的爆了,导致了RE。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;public class Main {private static class Hill {short x;short y;short z;Hill(short x, short y, short z) {this.x = x;this.y = y;this.z = z;}}public static void main(String[] args) throws IOException {BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));int num = Integer.parseInt(reader.readLine());Hill[] hills = new Hill[num];for (int i = 0; i < num; i++) {String[] temp = reader.readLine().split("\\s+");hills[i] = new Hill(Short.parseShort(temp[0]), Short.parseShort(temp[1]), Short.parseShort(temp[2]));}Arrays.sort(hills, Comparator.comparing(e -> e.z));reader.close();short x = hills[0].x, y = hills[0].y, z = hills[0].z;double sum = 0;for (int i = 1; i < num; i++) {sum += Math.sqrt(Math.pow(hills[i].x-x, 2) + Math.pow(hills[i].y-y, 2) + Math.pow(hills[i].z-z, 2));x = hills[i].x;y = hills[i].y;z = hills[i].z;}System.out.printf("%.3f", sum);}}

第四版代码(90分):

换回int的情况下,怀疑Math.pow()的效率问题,改成了直接相乘,解决了最后一个点,但倒数第二个点反而慢了一些。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;public class Main {private static class Hill {int x;int y;int z;Hill(int x, int y, int z) {this.x = x;this.y = y;this.z = z;}}public static void main(String[] args) throws IOException {BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));int num = Integer.parseInt(reader.readLine());Hill[] hills = new Hill[num];for (int i = 0; i < num; i++) {String[] temp = reader.readLine().split("\\s+");hills[i] = new Hill(Integer.parseInt(temp[0]), Integer.parseInt(temp[1]), Integer.parseInt(temp[2]));}Arrays.sort(hills, Comparator.comparing(e -> e.z));reader.close();int x = hills[0].x, y = hills[0].y, z = hills[0].z;double sum = 0;for (Hill hill : hills) {sum += Math.sqrt((hill.x-x)*(hill.x-x) + (hill.y-y)*(hill.y-y) + (hill.z-z)*(hill.z-z));x = hill.x;y = hill.y;z = hill.z;}System.out.printf("%.3f", sum);}}

第五版代码(90分):

尝试推翻sort()的约束,换成TreeSet来排序,但整体瓶颈点有所优化,倒数第二个点突破了,但最后一个点还差一点点。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;public class Main {private static class Hill {int x;int y;int z;Hill(int x, int y, int z) {this.x = x;this.y = y;this.z = z;}}public static void main(String[] args) throws IOException {BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));int num = Integer.parseInt(reader.readLine());Set<Hill> set = new TreeSet<>(Comparator.comparing(e -> e.z));for (int i = 0; i < num; i++) {String[] temp = reader.readLine().split("\\s+");set.add(new Hill(Integer.parseInt(temp[0]), Integer.parseInt(temp[1]), Integer.parseInt(temp[2])));}reader.close();Iterator<Hill> iterator = set.iterator();Hill hill0 = iterator.next();int x = hill0.x, y = hill0.y, z = hill0.z;double sum = 0;while (iterator.hasNext()) {Hill hill = iterator.next();sum += Math.sqrt((hill.x-x)*(hill.x-x) + (hill.y-y)*(hill.y-y) + (hill.z-z)*(hill.z-z));x = hill.x;y = hill.y;z = hill.z;}System.out.printf("%.3f", sum);}}

第六版代码(100分):

突然想到,是不是\\s+的匹配效率不如 ,就决定换一下,最终通过。没想到性能优化了这么多。

AC代码(Java语言描述)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;public class Main {private static class Hill {int x;int y;int z;Hill(int x, int y, int z) {this.x = x;this.y = y;this.z = z;}}public static void main(String[] args) throws IOException {BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));int num = Integer.parseInt(reader.readLine());Set<Hill> set = new TreeSet<>(Comparator.comparing(e -> e.z));for (int i = 0; i < num; i++) {String[] temp = reader.readLine().split(" ");set.add(new Hill(Integer.parseInt(temp[0]), Integer.parseInt(temp[1]), Integer.parseInt(temp[2])));}reader.close();Iterator<Hill> iterator = set.iterator();Hill hill0 = iterator.next();int x = hill0.x, y = hill0.y, z = hill0.z;double sum = 0;while (iterator.hasNext()) {Hill hill = iterator.next();sum += Math.sqrt((hill.x-x)*(hill.x-x) + (hill.y-y)*(hill.y-y) + (hill.z-z)*(hill.z-z));x = hill.x;y = hill.y;z = hill.z;}System.out.printf("%.3f", sum);}}

总结

由于这次只差一点点,所以并没有放弃Java,而是自己去琢磨,并总结出了如下经验:

  1. 使用\\s+的匹配效率不如 ,在保证可以使用空格的情况下可以直接用。
  2. 直接乘法的效率可能高于使用一次Math.pow(, 2)。
  3. sort()未必是最优策略,有时TreeSet也是一种好手段。
  4. 迭代器或者增强for循环的效率是优于普通for循环的。
  5. BufferedReader比Scanner固然是优秀很多的,但未必能完全解决问题。有的优化就在一些小细节上。

攀爬者(洛谷P5143题题解,Java语言描述)相关推荐

  1. 线性存储的最短平均检索时间(洛谷P1253题题解,Java语言描述)

    题目要求 P1253题目链接 分析 很像 ~洛谷P1223题题解~,也是一种类似SJF的贪心法. 排个序,由于两个不大于10000的数,乘起来还是int,就使用int属性吧. 数据量小,所以Scann ...

  2. 队列模拟约瑟夫问题(洛谷P1996题题解,Java语言描述)

    题目要求 P1996题目链接 分析 以前就研究过"约瑟夫环"问题: <单循环链表求解约瑟夫环问题(Java语言描述)> <杀人游戏~约瑟夫环(洛谷P1145题题解 ...

  3. 枚举求解单词方阵(洛谷P1101题题解,Java语言描述)

    题目要求 P1101题目链接 分析 可以用DFS做,但我立下了个Flag,所以就用了朴素的枚举来做.... 结果,我的天哪,做了好几个小时-- 其实这种地图题,真的适合 DFS or BFS or D ...

  4. 快速幂||取余运算【模板】(洛谷P1226题题解,Java语言描述)

    题目要求 P1226题目链接 分析 标准的快速幂取模算法板子,之前这个算法我在这篇文章中讲过了:<快速幂算法详解&&快速幂取模算法详解>. 这里选择使用比较简单的API实现 ...

  5. 贪心策略摘果子(洛谷P1478题题解,Java语言描述)

    题目要求 P1478题目链接 分析 本题的低配版题目链接 → 题解 那个题就是纯水题没啥可写的,我除了贴代码无话可说,但这题吧,虽然不算难,但也可一说. 建议大家移步这里 → 精辟题解 这位爷写了本题 ...

  6. 麦森数(洛谷P1045题题解,Java语言描述)

    题目要求 题目链接 分析 这题挺经典的,快速幂取模算法,如果求出大数再取模就可能T掉. 之前有篇文章写了这个算法:<快速幂算法详解&&快速幂取模算法详解> 既然是Java, ...

  7. 求子集元素之和(洛谷P2415题题解,Java语言描述)

    题目要求 P2415题目链接 分析 这题我觉得--当个数学题做就好了嘛. 有一个数N的情况:result = 1 * N 有两个数N1.N2的情况:result = 2 * (N1+N2) 有三个数N ...

  8. N进制正反累加判回文数(洛谷P1015题题解,Java语言描述)

    题目要求 P1015题目链接 分析 开始的时候写了这么一个代码,应该是比较基础的,是十进制的. private static void low() {Scanner scanner = new Sca ...

  9. 试试把OJ题意抽象成物理模型(洛谷P1007题题解,Java语言描述)

    题目要求 P1007题目链接 分析 这题干出的,真有毒... 要是有这种指挥官,也是醉了... 言归正传,这题其实是可以贪心求解的,但我们还可以更加666... 事实上,题干上面那一大篇基本在扯,关键 ...

最新文章

  1. 《虚拟化安全解决方案》一2.3 在Windows Server 2008上配置Microsoft Hyper-V
  2. RSA加密传输代码示例
  3. 值得分享!最新发现了10个冷门好用软件,一眼就会爱上
  4. BERT新转变:面向视觉基础进行预训练
  5. Spring事务传播性与隔离级别
  6. 《计算机科学概论》—第3章3.2节数字数据表示法
  7. 解决Windows接收IPSEC(IKE)流量的问题
  8. Xuggler开发教程
  9. 关于response格式转换
  10. centos: firewalld 一
  11. 学规划或GIS需要安装的软件
  12. 删除操作记录_微信消费记录能删吗?专家告诉你这样做百分百彻底删除!
  13. Raspberry Pi 构建一个飞机观察器
  14. vue中的传参的两种方式
  15. HFSS - 圆极化矩形微带天线设计与仿真
  16. GP数据库获取指定时段的所有天数
  17. matlab tic和toc单位,matlab toc tic 的用法
  18. 卡迪夫大数据专业排名_美国大学数据科学专业排名TOP66榜单
  19. 集成 rootbeer 和 小米mix2s Root 流程
  20. CSS详解(1.什么是css)

热门文章

  1. TP框架中的A方法和R方法
  2. php 5.5 xhprof for windows
  3. C++中的数组与指针
  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
  5. Android 安全机制概述
  6. 对二进制文件的操作(c++ 程序设计 by 谭浩强 课本实例)
  7. DelphiX教程系列 3 - 动画 part 1
  8. 机器学习基石-作业四-代码部分
  9. e5cc温控仪通讯参数设定_自动化工程师:施耐德 PLC常见两种编程通讯控制实例,收好不谢...
  10. java点赞功能实现_JavaWeb中点赞功能的实现及完整实例