xtu summer individual 5 F - Post Office
Post Office
This problem will be judged on PKU. Original ID: 1160
64-bit integer IO format: %lld Java class name: Main
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
Output
Sample Input
10 5
1 2 3 6 7 9 11 22 44 50
Sample Output
9
Source
#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的更多相关文章
- xtu summer individual 6 F - Water Tree
Water Tree Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Orig ...
- xtu summer individual 3 F - Opening Portals
Opening Portals Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- xtu summer individual 4 C - Dancing Lessons
Dancing Lessons Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- xtu summer individual 3 C.Infinite Maze
B. Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- xtu summer individual 2 E - Double Profiles
Double Profiles Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- xtu summer individual 2 C - Hometask
Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origin ...
- 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 ...
- xtu summer individual 2 D - Colliders
Colliders Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origi ...
- xtu summer individual 1 C - Design the city
C - Design the city Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu D ...
随机推荐
- Lightoj 1071 - Baker Vai (双线程DP)
题目连接: http://lightoj.com/volume_showproblem.php?problem=1071 题目大意: 一个n*m的格子,Baker Vai要从(1,1)到(n,m)再回 ...
- [hdu1695] GCD【莫比乌斯反演】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1695 先把题目转化为求一个数在区间[1, b / k],另一个数在区间[1, d / k]时,这两个数互 ...
- 扩展KMP的应用
扩展KMP的应用: 给出模板串S和串T,长度分别为Slen和Tlen,要求在线性时间内,对于每个S[i](0<=i<Slen),求出S[i..Slen-1]与T的 最长公共前缀长度,记为e ...
- C#中实现C++中的友元类
最近做一个小程序,一个类A(负责显示处理)需要大量调用类B(负责数据处理)的函数,我最先想到的C++中的友元概念,因为类B中的这些函数并不希望public,它只是允许类A调用监测. 网上搜索了一下,没 ...
- FileStream和BinaryReader,BinaryWriter,StreamReader,StreamWriter的区别
FileStream对于在文件系统上读取和写入文件非常有用,FileStream缓存输入和输出,以获得更好的性能.FileStream对象表示在磁盘或网络路径上指向文件的流.这个类提供了在文件中读写字 ...
- jmeter(十五)Jmeter默认报告优化
一.本文目的: 之前写了两篇文章搭建持续集成接口测试平台(Jenkins+Ant+Jmeter)和ANT批量执行Jmeter脚本,功能实现上都没有什么问题,但是最后生成的报告有一点小问题,虽然不影响使 ...
- 掌握Spark机器学习库-02-mllib数据格式
MLlib 1.MLlib介绍 1)MLlib特点 2)哪些算法 3)阅读官方文档 MLlib提供了哪些: 算法 特征工程 管道 持久化 2.MLlib数据格式 1)本地向量 2)标签数据 3)本地矩 ...
- Java语法基础-final关键字
final关键字主要用在三个地方:变量.方法.类. 对于一个final变量,如果是基本数据类型的变量,则其数值一旦在初始化之后便不能更改: 如果是引用类型的变量,则在对其初始化之后便不能再让其指向另一 ...
- web 自动化测试 selenium基础到应用(目录)
第一章 自动化测试前提及整体介绍 1-1功能测试和自动化测试的区别 1-2自动化测试流程有哪些 1-3自动化测试用例和手工用例的区别 1-4 自动化测试用例编写 1-5 selenium的优势以及 ...
- R in action读书笔记(12)第九章 方差分析
第九章方差分析 9.2 ANOVA 模型拟合 9.2.1 aov()函数 aov(formula, data = NULL, projections =FALSE, qr = TRUE, contra ...