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 ...
随机推荐
- Hdu 5379 Mahjong tree (dfs + 组合数)
题目链接: Hdu 5379 Mahjong tree 题目描述: 给出一个有n个节点的树,以节点1为根节点.问在满足兄弟节点连续 以及 子树包含节点连续 的条件下,有多少种编号方案给树上的n个点编号 ...
- Java Annontation(注解)详解
java中经常用到注解(Annontation),索性整理了下关于注解的相关知识点: 一.概念 Annontation是Java5开始引入的新特征,类似与.NET 中的attribute.中文名称一般 ...
- AJPFX关于网络编程的理解
1:网络编程(理解) (1)网络编程:用Java语言实现计算机间数据的信息传递和资源共享 (2)网络编程模型 (3)网络编程的三要素 ...
- 《effective java》中文第2版 note
,第15条[66]: 为不可变类提供静态工厂, eg : Integer/BigInteger 使用了静态工厂缓存了一些常用的实例, 通常 Integer -128 ~ +127. BigIntege ...
- hibernate与struts2整合中出现问题以及一级缓存查询数据在业务层问题
直接上问题: org.hibernate.HibernateException: HHH000142: Javassist Enhancement failed: cn.xxx.pojo.Custom ...
- idea 2018 解决 双击shift 弹出 search everywhere 搜索框的方法
https://youtrack.jetbrains.com/issue/IDEA-161094
- iOS Programming Introduction to Auto Layout 自动布局
iOS Programming Introduction to Auto Layout 自动布局 A single application that runs natively on both t ...
- [Windows Server 2012] MySQL移机方法
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:MySQL ...
- photoshop cs6安装和破解步骤
http://tieba.baidu.com/p/4791130877 http://www.frontopen.com/1181.html
- scrapy增加爬取效率
增加并发: 默认scrapy开启的并发线程为32个,可以适当进行增加.在settings配置文件中修改CONCURRENT_REQUESTS = 100值为100,并发设置成了为100. 降低日志级别 ...