攀爬者(洛谷P5143题题解,Java语言描述)
生活随笔
收集整理的這篇文章主要介紹了
攀爬者(洛谷P5143题题解,Java语言描述)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目要求
P5143題目鏈接
分析
一個排序求解的題,C++很容易AC,但Java黨需要做一些性能優(yōu)化,下面做一下分析和總結(jié)。
第一版代碼(80分):
第二版代碼(80分):
將Scanner換成了BufferedReader,個別點有所優(yōu)化,整體沒有突破性能瓶頸。
第三版代碼(80分):
使用short替換int,賭測試數(shù)據(jù)不會爆short,結(jié)果真的爆了,導(dǎo)致了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()的效率問題,改成了直接相乘,解決了最后一個點,但倒數(shù)第二個點反而慢了一些。
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來排序,但整體瓶頸點有所優(yōu)化,倒數(shù)第二個點突破了,但最后一個點還差一點點。
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+的匹配效率不如 ,就決定換一下,最終通過。沒想到性能優(yōu)化了這么多。
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);}}總結(jié)
由于這次只差一點點,所以并沒有放棄Java,而是自己去琢磨,并總結(jié)出了如下經(jīng)驗:
總結(jié)
以上是生活随笔為你收集整理的攀爬者(洛谷P5143题题解,Java语言描述)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深入理解 操作系统 SJF算法(以洛谷P
- 下一篇: 【大数据】大数据的特点