数字三角形
题目链接
https://www.acwing.com/problem/content/description/900/
题解
简单DP
#include<bits/stdc++.h>
using namespace std;
int a[550][550];
int main(){
int n;
cin >> n;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= i;j++){
cin >> a[i][j];
}
}
for(int i = n-1;i >= 1;i--){
for(int j = 1;j <= i;j++){
a[i][j] = max(a[i][j]+a[i+1][j],a[i][j] + a[i+1][j+1]);
}
}
cout << a[1][1];
return 0;
}
货仓选址
题目链接
https://www.acwing.com/problem/content/description/106/
题解
简单贪心,只需要将货仓放置放在将地址排序后的数组中心位置即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100005;
int a[N];
int main(){
int n;
ll s = 0;
cin >> n;
for(int i = 0;i < n;i++) cin >> a[i];
sort(a,a+n);
int x = n / 2;
for(int i = 0;i < n;i++) s += abs(a[i] - a[x]);
cout << s;
return 0;
}
本文介绍了两个编程题目,分别是求解数字三角形中最短路径的动态规划算法和货仓选址的贪心策略。对于数字三角形问题,通过自底向上更新每个节点的最大值来找到最短路径。而在货仓选址问题中,只需将货仓设在地址排序后的数组中心位置,以最小化所有地址到货仓的距离之和。
471

被折叠的 条评论
为什么被折叠?



