日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發布時間:2025/3/15 java 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 攀爬者(洛谷P5143题题解,Java语言描述) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目要求

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,而是自己去琢磨,并總結出了如下經驗:

  • 使用\\s+的匹配效率不如 ,在保證可以使用空格的情況下可以直接用。
  • 直接乘法的效率可能高于使用一次Math.pow(, 2)。
  • sort()未必是最優策略,有時TreeSet也是一種好手段。
  • 迭代器或者增強for循環的效率是優于普通for循環的。
  • BufferedReader比Scanner固然是優秀很多的,但未必能完全解決問題。有的優化就在一些小細節上。
  • 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

    總結

    以上是生活随笔為你收集整理的攀爬者(洛谷P5143题题解,Java语言描述)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。