数字三角形
题目链接
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;
}