L2-11
解题思路:二叉树的构建和遍历
参考代码:
L2-12
解题思路:
参考代码:
L2-13
解题思路:
参考代码:
L2-14 列车调度
解题思路:求把数列分成最少的递减子序列,既是求最长子序列长度,使用二分贪心即可在复杂度求出
参考代码:
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n), d(n + 1, 0);
for (int i = 0; i < n; ++i) cin >> a[i];
int len = 1;
d[len] = a[0];
for (int i = 1; i < n; ++i) {
if (a[i] > d[len]) d[++len] = a[i];
else {
int l = 1, r = len, pos = 0;
while (l <= r) {
int mid = (l + r) >> 1;
if (d[mid] < a[i]) {
pos = mid;
l = mid + 1;
} else r = mid - 1;
}
d[pos + 1] = a[i];
}
}
cout << len << endl;
return 0;
}
L2-15 互评成绩
解题思路:简单排序
参考代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
double x[N];
int n, k, m;
double a[N][15];
int main() {
cin >> n >> k >> m;
for (int i = 1; i <= n; i++) {
double s = 0;
double mi = 999, ma = -1;
for (int j = 1; j <= k; j++) {
cin >> a[i][j];
s += a[i][j];
mi = min(mi, a[i][j]);
ma = max(ma, a[i][j]);
}
x[i] = (s - mi - ma) / (k - 2);
}
sort(x + 1, x + n + 1);
for (int i = n - m + 1; i < n; i++) printf("%.3lf ", x[i]);
printf("%.3lf\n", x[n]);
return 0;
}