Post Office
Time Limit: 1000ms
Memory Limit: 10000KB
This problem will be judged on PKU. Original ID: 1160
64-bit integer IO format: %lld Java class name: Main
There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each village is identified with a single integer coordinate. There are no two villages in the same position. The distance between two positions is the absolute value of the difference of their integer coordinates.
Post offices will be built in some, but not necessarily all of the villages. A village and the post office in it have the same position. For building the post offices, their positions should be chosen so that the total sum of all distances between each village and its nearest post office is minimum.
You are to write a program which, given the positions of the villages and the number of post offices, computes the least possible sum of all distances between each village and its nearest post office.
Input
Your program is to read from standard input. The first line contains two integers: the first is the number of villages V, 1 <= V <= 300, and the second is the number of post offices P, 1 <= P <= 30, P <= V. The second line contains V integers in increasing order. These V integers are the positions of the villages. For each position X it holds that 1 <= X <= 10000.
Output
The first line contains one integer S, which is the sum of all distances between each village and its nearest post office.
Sample Input
10 5
1 2 3 6 7 9 11 22 44 50
Sample Output
9
Source
解题:dp,dp[i][j]表示i个邮局负责j个村子时的最短距离和。距离和最少?肯定是选取的点如果在第i个与第n-i 个村子的中点(1 <= 1 <= n/2),距离和会最小啊!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
int dp[][],x[],n,m;
int dis(int i,int j){
int sum = ;
while(i < j) sum += x[j--]-x[i++];
return sum;
}
int main(){
int i,j,k;
while(~scanf("%d%d",&m,&n)){
memset(dp,,sizeof(dp));
for(i = ; i <= m; i++){
scanf("%d",x+i);
dp[][i] = dis(,i);
}
for(i = ; i <= n; i++){
for(j = i; j <= m; j++){
dp[i][j] = INF;
for(k = i-; k < j; k++)
dp[i][j] = min(dp[i][j],dp[i-][k]+dis(k+,j));
}
}
cout<<dp[n][m]<<endl;
}
return ;
}
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ;
int w[maxn][maxn],dp[maxn][maxn],a[maxn],n,m;
int main() {
while(~scanf("%d%d",&n,&m)) {
for(int i = ; i <= n; ++i) scanf("%d",a + i);
memset(w,,sizeof w);
for(int i = ; i <= n; ++i) {
for(int j = i + ; j <= n; ++j)
w[i][j] = w[i][j-] + a[j] - a[(i+j)>>];
}
for(int i = ; i <= n; ++i) {
dp[i][i] = ;
dp[i][] = w[][i];
}
for(int j = ; j <= m; ++j) {
for(int i = j + ; i <= n; ++i) {
dp[i][j] = INF;
for(int k = j-; k < i; ++k)
dp[i][j] = min(dp[i][j],dp[k][j-] + w[k+][i]);
}
}
printf("%d\n",dp[n][m]);
}
return ;
}
还是太年轻,蓝桥杯决赛有题就是这个,当时做的时候,没有理解这题的精髓,太傻了
平行四边形优化
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ;
int w[maxn][maxn],dp[maxn][maxn],a[maxn],s[maxn][maxn],n,m;
int main() {
while(~scanf("%d%d",&n,&m)) {
for(int i = ; i <= n; ++i) scanf("%d",a + i);
memset(w,,sizeof w);
for(int i = ; i <= n; ++i) {
for(int j = i + ; j <= n; ++j)
w[i][j] = w[i][j-] + a[j] - a[(i+j)>>];
}
for(int i = ; i <= n; ++i) {
dp[i][i] = ;
dp[i][] = w[][i];
s[i][] = ;
}
for(int j = ; j <= m; ++j) {
s[n+][j] = n;
for(int i = n; i > j; --i) {
dp[i][j] = INF;
for(int k = s[i][j-]; k <= s[i+][j]; ++k){
int tmp = dp[k][j-] + w[k+][i];
if(tmp < dp[i][j]){
dp[i][j] = tmp;
s[i][j] = k;
}
}
}
}
printf("%d\n",dp[n][m]);
}
return ;
}