From 573c77674e97857c8689e24496577aac624bd3bf Mon Sep 17 00:00:00 2001 From: HuangHai <10402852@qq.com> Date: Sun, 5 Oct 2025 18:33:03 +0800 Subject: [PATCH] 'commit' --- TangDou/CSP-J/AcWingTraining/T1/442.md | 115 ++++++++++++++++++++++ TangDou/CSP-J/AcWingTraining/T1/442_2.cpp | 28 ++++++ TangDou/Test/TD.cpp | 42 ++++---- 3 files changed, 160 insertions(+), 25 deletions(-) create mode 100644 TangDou/CSP-J/AcWingTraining/T1/442.md create mode 100644 TangDou/CSP-J/AcWingTraining/T1/442_2.cpp diff --git a/TangDou/CSP-J/AcWingTraining/T1/442.md b/TangDou/CSP-J/AcWingTraining/T1/442.md new file mode 100644 index 0000000..6da6cf1 --- /dev/null +++ b/TangDou/CSP-J/AcWingTraining/T1/442.md @@ -0,0 +1,115 @@ + +## 排队接水 + +### 一、数学证明 +首先我们要排的是所有的元素,但是为什么是从小到大呢??? + +排队总是象征要排序,每个元素在此序列下都要满足条件,也就是从中拿出两个相邻元素同样满足条件: + +于是设: + +$a_i$ 和 $b_i$且$a_i + +using namespace std; +const int N = 10010, M = 200; +int n, m; +int w[N]; +int q[M]; + +int main() { + cin >> n >> m; + for (int i = 0; i < n; i++) + cin >> w[i]; + + for (int i = 0; i < n; i++) { + // 找出最小值 + int t = 0; + for (int j = 0; j < m; j++) { + if (q[j] < q[t]) + t = j; + } + // 将i号同学安排到第t个位置上 + q[t] += w[i]; + } + int res = 0; + for (int i = 0; i < m; i++) + res = max(res, q[i]); // 找出最大值就是答案 + cout << res << endl; + return 0; +} +``` + +### 三、变形 + +**【题目描述】** +有$n$个人在一个水龙头前排队接水,假如每个人接水的时间为$T_i$,请编程找出这$n$个人排队的一种顺序,使得$n$个人的平均等待时间最小。 + +**【输入】** +共两行,第一行为$n(1≤n≤1000)$;第二行分别表示第$1$个人到第$n$个人每人的接水时间$T_1,T_2,…,T_n$,每个数据之间有$1$个空格。 + +**【输出】** +有两行,第一行为一种排队顺序,即$1$到$n$的一种排列;第二行为这种排列方案下的平均等待时间(输出结果精确到小数点后两位)。 + +**【输入样例】** +``` +10 +56 12 1 99 1000 234 33 55 99 812 +``` +**【输出样例】** +``` +3 2 7 8 1 4 9 6 10 5 +291.90 +``` + +``` +http://ybt.ssoier.cn:8088/problem_show.php?pid=1319 +``` + +```cpp {.line-numbers} +#include +using namespace std; +const int N = 1005; + +struct Node { + int num; + int sum; +} a[N]; +bool cmp(Node x, Node y) { + return x.sum < y.sum; +} +int main() { + int n; + cin >> n; + for (int i = 0; i < n; i++) { + cin >> a[i].sum; + a[i].num = i + 1; + } + sort(a, a + n, cmp); + double temp = 0.00; + for (int i = 0; i < n; i++) { + cout << a[i].num << " "; + temp += (n - (i + 1)) * a[i].sum; + } + cout << endl; + printf("%.2f", temp / n * 1.00); + return 0; +} +``` \ No newline at end of file diff --git a/TangDou/CSP-J/AcWingTraining/T1/442_2.cpp b/TangDou/CSP-J/AcWingTraining/T1/442_2.cpp new file mode 100644 index 0000000..208163c --- /dev/null +++ b/TangDou/CSP-J/AcWingTraining/T1/442_2.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; +const int N = 1005; + +struct Node { + int num; + int sum; +} a[N]; +bool cmp(Node x, Node y) { + return x.sum < y.sum; +} +int main() { + int n; + cin >> n; + for (int i = 0; i < n; i++) { + cin >> a[i].sum; + a[i].num = i + 1; + } + sort(a, a + n, cmp); + double temp = 0.00; + for (int i = 0; i < n; i++) { + cout << a[i].num << " "; + temp += (n - (i + 1)) * a[i].sum; + } + cout << endl; + printf("%.2f", temp / n * 1.00); + return 0; +} diff --git a/TangDou/Test/TD.cpp b/TangDou/Test/TD.cpp index 8e1cccc..b4b107c 100644 --- a/TangDou/Test/TD.cpp +++ b/TangDou/Test/TD.cpp @@ -1,31 +1,23 @@ #include using namespace std; -long long n; +int w[1001]; +int s[1001]; int main() { - cin >> n; - bool first = true; - for (long long p = 2;p*p<=n;p++){ - if (n % p != 0) continue; - int cnt = 0; - while (n % p == 0){ - n /= p; - cnt++; - } - if (first){ - first = false; - cout << p; - }else{ - cout << " * " << p; - } - if (cnt > 1) cout << " ^ " << cnt; + int n, m; + cin >> n >> m; + for (int i = 1; i <= n; i++) { + cin >> w[i]; } - if (n != 1){ - if (first){ - first = false; - cout << n; - }else{ - cout << " * " << n; - } + int ans = 0; + sort(w + 1, w + 1 + n); + int j = 0; + for(int i = 1; i <= n; i++){ + j++; + if (j == m+ 1) j = 1; + s[j] += w[i]; + ans += s[j]; } - return 0; + + cout << ans << endl; + return 0; } \ No newline at end of file