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. Zoj 1610 Count the Colors (线段树+区间更新+暴力计数)

    题目大意: 有n次操作,每次都是对一根线中的一段区间进行染色(颜色并不相同),有时候后面的颜色有可能覆盖前面的颜色,问最后涂完色,能看到的颜色有几种,每种颜色有几部分? 解题思路: 这个题目建树的时候 ...

  2. shell脚本从入门到精通

    阿里云大学 shell脚本从入门到精通 第1 章 : shell脚本编程-变量-算术表达式-判断语句-if分支语句 第2 章 : case-for-While-双括号-循环嵌套-break-conti ...

  3. GUID的学习

    GUID(全局统一标识符)是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的.通常平台会提供生成GUID的API.生成算法很有意思,用到了以太网卡地址.纳秒级时间.芯片ID码和许多可 ...

  4. iOS- NSThread/NSOperation/GCD 三种多线程技术的对比及实现 -- 转

    1.iOS的三种多线程技术 1.NSThread 每个NSThread对象对应一个线程,量级较轻(真正的多线程) 2.以下两点是苹果专门开发的“并发”技术,使得程序员可以不再去关心线程的具体使用问题 ...

  5. Rocketmq Broker启动网卡顺序问题

    方法一.修改网卡名称,因为网卡顺序是通过名称排列的 方法二.指定broker使用IP echo "brokerIP1=192.168.1.220" > conf/broker ...

  6. Suricata的所有运行方式模式(图文详解)

    不多说,直接上干货! suricata的基本组成.Suricata是由所谓的线程(threads).线程模块 (thread-modules)和队列(queues)组成.Suricata是一个多线程的 ...

  7. Windowsforms 中 进程,线程

    进程: 进程是一个具有独立功能的程序关于某个数据集合的一次运行活动. 它可以申请和拥有系统资源,是一个动态的概念,是一个活动的实体. Process 类,用来操作进程. 命名空间:using Syst ...

  8. JavaScript - try catch finally throw

    语法: try { tryCode - 尝试执行代码块 } catch(err) { catchCode - 捕获错误的代码块 } finally { finallyCode - 无论 try / c ...

  9. CCF|跳一跳

    import java.util.Scanner; public class Main { public static void main (String[] args) { Scanner scan ...

  10. RGB、YUV和YCbCr介绍【转】

    RGB: 就是常说的红(Red).绿(Green)和蓝(Blue),每个图像的像素点由RGB三个通道的值组成. YUV和YCbCr: YUV与RGB的转换: Y'= 0.299*R' + 0.587* ...