2025-7-15-C++ 学习 排序(4)
文章目录
- 2025-7-15-C++ 学习 排序(4)
- P5143 攀爬者
- 题目背景
- 题目描述
- 输入格式
- 输出格式
- 输入输出样例 #1
- 输入 #1
- 输出 #1
- 说明/提示
- 提交代码
- P1104 生日
- 题目描述
- 输入格式
- 输出格式
- 输入输出样例 #1
- 输入 #1
- 输出 #1
- 说明/提示
- 提交代码
- P1012 [NOIP 1998 提高组] 拼数
- 题目描述
- 输入格式
- 输出格式
- 输入输出样例 #1
- 输入 #1
- 输出 #1
- 输入输出样例 #2
- 输入 #2
- 输出 #2
- 说明/提示
- 提交代码
2025-7-15-C++ 学习 排序(4)
排序,end。
P5143 攀爬者
题目背景
HKE 考完 GDOI 之后跟他的神犇小伙伴们一起去爬山。
题目描述
他在地形图上标记了 NNN 个点,每个点 PiP_iPi 都有一个坐标 (xi,yi,zi)(x_i,y_i,z_i)(xi,yi,zi)。所有点对中,高度值 zzz 不会相等。HKE 准备从最低的点爬到最高的点,他的攀爬满足以下条件:
(1) 经过他标记的每一个点;
(2) 从第二个点开始,他经过的每一个点高度 zzz 都比上一个点高;
(3) HKE 会飞,他从一个点 PiP_iPi 爬到 PjP_jPj 的距离为两个点的欧几里得距离。即,(Xi−Xj)2+(Yi−Yj)2+(Zi−Zj)2\sqrt{(X_i-X_j)^2+(Y_i-Y_j)^2+(Z_i-Z_j)^2}(Xi−Xj)2+(Yi−Yj)2+(Zi−Zj)2
现在,HKE 希望你能求出他攀爬的总距离。
输入格式
第一行,一个整数 NNN 表示地图上的点数。
接下来 NNN 行,三个整数 xi,yi,zix_i,y_i,z_ixi,yi,zi 表示第 iii 个点的坐标。
输出格式
一个实数,表示 HKE 需要攀爬的总距离(保留三位小数)
输入输出样例 #1
输入 #1
5
2 2 2
1 1 1
4 4 4
3 3 3
5 5 5
输出 #1
6.928
说明/提示
对于100%的数据,1≤N≤500001\leq N\leq 500001≤N≤50000,答案的范围在 double 范围内。
提交代码
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;struct Point {int x, y, z;
};bool compare(const Point& a, const Point& b) {return a.z < b.z;
}double distance(const Point& a, const Point& b) {return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2) + pow(a.z - b.z, 2));
}int main() {int n;cin >> n;vector<Point> points(n);for (int i = 0; i < n; i++) {cin >> points[i].x >> points[i].y >> points[i].z;}sort(points.begin(), points.end(), compare);double totalDistance = 0.0;for (int i = 0; i < n - 1; i++) {totalDistance += distance(points[i], points[i + 1]);}cout << fixed << setprecision(3) << totalDistance << endl;return 0;
}
P1104 生日
题目描述
cjf 君想调查学校 OI 组每个同学的生日,并按照年龄从大到小的顺序排序。但 cjf 君最近作业很多,没有时间,所以请你帮她排序。
输入格式
输入共有 n+1n + 1n+1 行,
第 111 行为 OI 组总人数 nnn;
第 222 行至第 n+1n+1n+1 行分别是每人的姓名 sss、出生年 yyy、月 mmm、日 ddd。
输出格式
输出共有 nnn 行,
即 nnn 个生日从大到小同学的姓名。(如果有两个同学生日相同,输入靠后的同学先输出)
输入输出样例 #1
输入 #1
3
Yangchu 1992 4 23
Qiujingya 1993 10 13
Luowen 1991 8 1
输出 #1
Luowen
Yangchu
Qiujingya
说明/提示
数据保证,1<n<1001<n<1001<n<100,1≤∣s∣<201\leq |s|<201≤∣s∣<20。保证年月日实际存在,且年份 ∈[1960,2020]\in [1960,2020]∈[1960,2020]。
提交代码
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;struct Student {string name;int year, month, day;int order;
};bool compare(const Student& a, const Student& b) {if (a.year != b.year) return a.year < b.year;if (a.month != b.month) return a.month < b.month;if (a.day != b.day) return a.day < b.day;return a.order > b.order;
}int main() {int n;cin >> n;vector<Student> students(n);for (int i = 0; i < n; i++) {cin >> students[i].name >> students[i].year >> students[i].month >> students[i].day;students[i].order = i;}sort(students.begin(), students.end(), compare);for (const auto& s : students) {cout << s.name << endl;}return 0;
}
P1012 [NOIP 1998 提高组] 拼数
题目描述
设有 nnn 个正整数 a1…ana_1 \dots a_na1…an,将它们联接成一排,相邻数字首尾相接,组成一个最大的整数。
输入格式
第一行有一个整数,表示数字个数 nnn。
第二行有 nnn 个整数,表示给出的 nnn 个整数 aia_iai。
输出格式
一个正整数,表示最大的整数
输入输出样例 #1
输入 #1
3
13 312 343
输出 #1
34331213
输入输出样例 #2
输入 #2
4
7 13 4 246
输出 #2
7424613
说明/提示
对于全部的测试点,保证 1≤n≤201 \leq n \leq 201≤n≤20,1≤ai≤1091 \leq a_i \leq 10^91≤ai≤109。
NOIP1998 提高组 第二题
提交代码
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>using namespace std;bool compare(const string &a, const string &b) {return a + b > b + a;
}int main() {int n;cin >> n;vector<string> nums(n);for (int i = 0; i < n; i++) {cin >> nums[i];}sort(nums.begin(), nums.end(), compare);string result;for (const string &num : nums) {result += num;}cout << (result[0] == '0' ? "0" : result) << endl;return 0;
}