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 ;
}

xtu summer individual 5 F - Post Office的更多相关文章

  1. xtu summer individual 6 F - Water Tree

    Water Tree Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Orig ...

  2. xtu summer individual 3 F - Opening Portals

    Opening Portals Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  3. xtu summer individual 4 C - Dancing Lessons

    Dancing Lessons Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  4. xtu summer individual 3 C.Infinite Maze

    B. Infinite Maze time limit per test  2 seconds memory limit per test  256 megabytes input standard ...

  5. xtu summer individual 2 E - Double Profiles

    Double Profiles Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  6. xtu summer individual 2 C - Hometask

    Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origin ...

  7. xtu summer individual 1 A - An interesting mobile game

    An interesting mobile game Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on H ...

  8. xtu summer individual 2 D - Colliders

    Colliders Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origi ...

  9. xtu summer individual 1 C - Design the city

    C - Design the city Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu D ...

随机推荐

  1. 题解报告:hdu1202The calculation of GPA(算绩点问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1202 Problem Description 每学期的期末,大家都会忙于计算自己的平均成绩,这个成绩对 ...

  2. Java Annontation(注解)详解

    java中经常用到注解(Annontation),索性整理了下关于注解的相关知识点: 一.概念 Annontation是Java5开始引入的新特征,类似与.NET 中的attribute.中文名称一般 ...

  3. 转 Oracle 12c: Managing Resources

    http://www.oracle-class.com/?p=3058 1. Introduction: Oracle database 12c comes with several Resource ...

  4. WPF学习10:基于MVVM Light 制作图形编辑工具(1)

    图形编辑器的功能如下图所示: 除了MVVM Light 框架是一个新东西之外,本文所涉及内容之前的WPF学习0-9基本都有相关介绍. 本节中,将搭建编辑器的界面,搭建MVVM Light 框架的使用环 ...

  5. Spring注解驱动开发之Ioc容器篇

    前言:现今SpringBoot.SpringCloud技术非常火热,作为Spring之上的框架,他们大量使用到了Spring的一些底层注解.原理,比如@Conditional.@Import.@Ena ...

  6. AJPFX关于抽象方法和接口

    class Demo_Animal1{ public static void main(String[] args) {                Cat a = new Cat("加菲 ...

  7. JDK11源码分析之集合类(一)----HashMap

    一,首先需要拉取JDK11源码: 方便起见我给出芋道源码作者已经拉取好的openJDK11的GitHub地址只需要fork一下克隆到本地导入IDEA中就可以对源码分析了: https://github ...

  8. Django系列:(1)PyCharm下创建并运行我们的第一个Django工程

    准备工作: 假设读者已经安装好python 2x或3x,以及安装好Django,以及Pycharm. 我的配置: – Python 2.7.11 – Pycharm Professional 5.0. ...

  9. iOS/Android 视频编辑SDK

    锐动天地为开发者提供短视频编辑.特效.直播.录屏.编解码.视频转换,等多种解决方案,涵盖PC.iOS.Android多平台.以市场为导向,不断打磨并创新技术,在稳定性,兼容性,硬件设备效率优化上千捶百 ...

  10. [小记]Android缓存问题

    今天晚上,产品经理打电话说我们的Android App除了问题,问题很简单就是一个缓存问题,由于这个程序是前同事写的,我也只能呵呵一笑,有些事你就得扛.还是回到正题吧,这个缓存问题,实在有点奇葩,所以 ...