本文主要是介绍三个算法题之---bjtu算法期末小测验,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
链接地址:https://algo.bjtu.edu.cn/contest?group=my
题目1:Problem A. 加分二叉树
时间限制 1000 ms
内存限制 128 MB
题目描述设一个n个节点的二叉树tree的中序遍历为(l,2,3,⋯,n),其中数字1,2,3,⋯,n为节点编号。每个节点都有一个分数(均为正整数),记第i个节点的分数为di,tree及它的每个子树都有一个加分,任一棵子树subtree(也包含tree本身)的加分计算方法如下:subtree的左子树的加分× subtree的右子树的加分+subtree的根的分数若某个子树为空,规定其加分为1,叶子的加分就是叶节点本身的分数。不考虑它的空子树。试求一棵符合中序遍历为(1,2,3,⋯,n)且加分最高的二叉树tree。要求输出;(1)tree的最高加分(2)tree的前序遍历输入数据第 1行:一个整数 n (n<30),为节点个数。第 2行 :n个用空格隔开的整数,为每个节点的分数(分数 <100)。
输出数据第 1行:一个整数,为最高加分(结果不会超过 4,000,000,000)。第 2行 :n个用空格隔开的整数,为该树的前序遍历。
若存在多种前序遍历均为最高加分,则输出字典序最小的前序遍历
样例输入
5
5 7 1 2 10
样例输出
145
3 1 2 4 5题目2:Problem B. 思维之花-方程
时间限制 1000 ms
内存限制 128 MB
题目描述有形如:ax^3+bx^2+cx+d=0 这样的一个一元三次方程。给出该方程中各项的系数(a,b,c,d 均为实数),并约定该方程存在三个不同实根(根的范围在-100至100之间),且根与根之差的绝对值> =1。要求由小到大依次在同一行输出这三个实根(根与根之间留有空格),并精确到小数点后2位。提示:记方程f(x)=0,若存在2个数x1和x2,且x1< x2,f(x1)*(x2)< 0,则在(x1,x2)之间一定有一个根。输入数据
输入该方程中各项的系数 (a,b,c,d均为实数),
输出数据
由小到大依次在同一行输出这三个实根(根与根之间留有空格),并精确到小数点后 2位。
样例输入
1 -5 -4 20
样例输出
-2.00 2.00 5.00题目3:Problem C. 旅行家的预算
时间限制 1000 ms
内存限制 128 MB
题目描述
一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市(假设出发时油箱是空的)。给定两个城市之间的距离d1、汽车油箱的容量c(以升为单位)、每升汽油能行驶的距离d2、出发点每升汽油价格p和沿途油站数n,油站i离出发点的距离d[i]、每升汽油价格p[i]。
计算结果四舍五入至小数点后两位。
如果无法到达目的地,则输出-1。输入数据
输入共 n+1行,第一行为 d1,c,d2,p,n,以下 n行,每行两个数据,分别表示该油站距出发点的距离 d[i]和该油站每升汽油的价格 p[i]。两个数据之间用一个空格隔开。
输出数据
1 < = n < = 100
样例输入
275.6 11.9 27.4 2.8 2
102.0 2.9
220.0 2.2
样例输出
26.95
样例说明
0< =n< =100
例子1:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string.h>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
inline ll read() {ll x = 0, w = 1; char ch = getchar();for (; ch > '9' || ch < '0'; ch = getchar()) if (ch == '-') w = -1;for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';return x * w;
}
#define maxn 1000000
ll f[100][100], rt[100][100];
ll dfs(int l, int r) {if (l > r) return 1;if (f[l][r] == -1) {for (int k = l; k <= r; k++) {ll ans = dfs(l, k - 1) * dfs(k + 1, r) + f[k][k];if (ans > f[l][r]) {f[l][r] = ans;rt[l][r] = k;}}}return f[l][r];
}
void houxubianli(int l, int r) {if (l == r) {cout << rt[l][r] << " ";return;}if (l > r) return;cout << rt[l][r] << " ";houxubianli(l, rt[l][r] - 1);houxubianli(rt[l][r] + 1, r);
}
int main() {int n; int a;n = read();for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {f[i][j] = -1;}}for (int i = 1; i <= n; i++) {rt[i][i] = i;f[i][i] = read();}dfs(1, n);cout << f[1][n] << endl;houxubianli(1, n);
}
例子2:
#include<bits/stdc++.h>
using namespace std;
double a, b, c, d;
double f(double x) {return (a * x * x * x + b * x * x + c * x + d);
}
int main() {cin >> a >> b >> c >> d;for (int i = -100; i <= 100; i++) { double L = i, R = i + 1, mid;if (fabs(f(L)) < 1e-4) printf("%.2lf ", L);else if (fabs(f(R)) < 1e-4) continue;else if (f(L) * f(R) < 0) { while (R - L > 1e-4) {mid = (L + R) / 2;if (f(mid) * f(R) > 0)R = mid;else L = mid;}printf("%.2lf ", L);}}return 0;
}
例子3:
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<cmath>
#include<queue>
#include<stdlib.h>
#include<map>
#include<vector>
#include<cstdio>
#define ll long long
using namespace std;
struct node {double dis, cost;
}x[101];
int main() {double d1, c, d2;int n;x[0].dis = 0;cin >> d1 >> c >> d2 >> x[0].cost >> n;for (int i = 1; i <= n; ++i)cin >> x[i].dis >> x[i].cost;x[n + 1].dis = d1;x[n + 1].cost = 0;for (int i = 1; i <= n + 1; ++i) {if (x[i].dis - x[i - 1].dis > c * d2) { cout << "-1" << endl;return 0;}}double sum = 0; double w = 0; int i = 0, p;while (i <= n) {for (int j = i + 1; j <= n + 1; ++j) {if (x[j].cost < x[i].cost) {p = j;break;}}if (x[p].dis - x[i].dis > c * d2) {sum += (c - w) * x[i].cost;w = c - (x[i + 1].dis - x[i].dis) / d2;i++;}else if (w * d2 >= x[p].dis - x[i].dis) {w -= (x[p].dis - x[i].dis) / d2;i = p;}else {sum += ((x[p].dis - x[i].dis) / d2 - w) * x[i].cost;w = 0;i = p;}}printf("%.2lf", sum);return 0;
}
这篇关于三个算法题之---bjtu算法期末小测验的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!