题目传送门

题意:n个饭店在一条直线上,给了它们的坐标,现在要建造m个停车场,饭店没有停车场的要到最近的停车场,问所有饭店到停车场的最短距离

分析:易得区间(i, j)的最短距离和一定是建在(i + j) / 2的饭店,预处理出(i, j)的距离和sum[i][j],mark[i][j] 表示区间的最优停车场的位置,mid[i][j]表示(i + j) / 2。状态转移方程:dp[i][j] = max (dp[k-1][j-1] + sum[k][i]);

收获:学习递归打印路径

代码:

/************************************************
* Author :Running_Time
* Created Time :2015-8-29 16:42:33
* File Name :UVA_662.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 2e2 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int dp[N][33];
int sum[N][N], d[N], mark[N][N], mid[N][N];
int n, m;
int idx; void print(int i, int j) {
if (i < 1 || j < 1) return ;
print (mark[i][j]-1, j-1);
printf ("Depot %d at restaurant %d serves restaurant", ++idx, mid[mark[i][j]][i]);
if (mark[i][j] == i) {
printf (" %d\n", i); return ;
}
else printf ("s %d to %d\n", mark[i][j], i);
} int main(void) {
int cas = 0;
while (scanf ("%d%d", &n, &m) == 2) {
if (!n && !m) break;
for (int i=1; i<=n; ++i) scanf ("%d", &d[i]);
memset (sum, 0, sizeof (sum));
for (int i=1; i<=n; ++i) {
mid[i][i]= i;
for (int j=i+1; j<=n; ++j) {
int mm = (i + j) >> 1;
mid[i][j] = mm;
for (int k=i; k<=j; ++k) {
sum[i][j] += abs (d[mm] - d[k]);
}
}
}
memset (dp, INF, sizeof (dp));
memset (dp[0], 0, sizeof (dp[0]));
for (int i=1; i<=n; ++i) {
for (int j=1; j<=m; ++j) {
for (int k=1; k<=i; ++k) {
int tmp = dp[k-1][j-1] + sum[k][i];
if (dp[i][j] >= tmp) {
dp[i][j] = tmp;
mark[i][j] = k;
}
}
}
}
printf ("Chain %d\n", ++cas);
idx = 0; print (n, m);
printf ("Total distance sum = %d\n\n", dp[n][m]);
} return 0;
}

  

DP(递归打印路径) UVA 662 Fast Food的更多相关文章

  1. poj 1141 区间dp+递归打印路径

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30383   Accepted: 871 ...

  2. UVA 1626 区间dp、打印路径

    uva 紫书例题,这个区间dp最容易错的应该是(S)这种匹配情况,如果不是题目中给了提示我就忽略了,只想着左右分割忘记了这种特殊的例子. dp[i][j]=MIN{dp[i+1][j-1] | if( ...

  3. UVA 531 - Compromise(dp + LCS打印路径)

      Compromise  In a few months the European Currency Union will become a reality. However, to join th ...

  4. FatMouse's Speed ~(基础DP)打印路径的上升子序列

    FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take ...

  5. UVA 10054 The Necklace(欧拉回路,打印路径)

    题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  6. zoj 3088 Easter Holidays(最长路+最短路+打印路径)

    Scandinavians often make vacation during the Easter holidays in the largest ski resort Are. Are prov ...

  7. L2-001. 紧急救援 (Dijkstra算法打印路径)

    作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上.当其他城市有紧急求 ...

  8. UVa 103 - Stacking Boxes (LIS,打印路径)

    链接:UVa 103 题意:给n维图形,它们的边长是{d1,d2,d3...dn},  对于两个n维图形,求满足当中一个的全部边长 依照随意顺序都一一相应小于还有一个的边长,这种最长序列的个数,而且打 ...

  9. Uva 10131 Is Bigger Smarter? (LIS,打印路径)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=1072">链接:UVa 10131 题意: ...

随机推荐

  1. 【转】C++函数的重载、覆盖和隐藏区别

    网上看到的关于C++函数的重载.覆盖和隐藏区别的回答,如下(其内容来源于C++面试宝典中一道题目): a.成员函数被重载的特征:(1)相同的范围(在同一个类中):(2)函数名字相同:(3)参数不同:( ...

  2. samba.conf (香港中华厨房有限公司实例)

    # Sample configuration file for the Samba suite for Debian GNU/Linux. # # This is the main Samba con ...

  3. 九度OJ1004 Median

    题目描写叙述: Given an increasing sequence S of N integers, the median is the number at the middle positio ...

  4. Java 文件路径的读取

    记得在操作系统中了解到文件读取有两种方式,当然这在各编程语言中也是通用的,所以java路径也分,相对和绝对路径. 绝对路径 绝对路径URI ,听着和URL非常相似.那我们就来看看吧. URI(Unif ...

  5. 【翻译自mos文章】在12c中Create or Truncate Table时非常慢,等待事件为 DFS Lock Handle wait

    来源于: Create or Truncate Table Slow in 12c While Waiting for DFS Lock Handle wait (文档 ID 2085308.1) A ...

  6. Java类加载机制?

    深入研究Java类加载机制 类加载是Java程序运行的第一步,研究类的加载有助于了解JVM执行过程,并指导开发者采取更有效的措施配合程序执行. 研究类加载机制的第二个目的是让程序能动态的控制类加载,比 ...

  7. HDU 5056 Boring count(不超过k个字符的子串个数)

    Boring count Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  8. YTU 2912: 圆柱体的C++

    2912: 圆柱体的C++ 时间限制: 1 Sec  内存限制: 128 MB 提交: 333  解决: 133 题目描述 小明的弟弟加入的C++兴趣小组,组长布置的第一个任务就是将已有的C程序改写成 ...

  9. html5--7-33 阶段练习5

    html5--7-33 阶段练习5 总结: 1.JS中可以递归函数 2.js中数组对象array的使用 学习要点 综合运用学过的知识完成三个综合小练习,巩固学过的知识. 阶段小练习5-1:使用递归算法 ...

  10. 如何将Eclipse中的项目迁移到Android Studio中

    如果你之前有用Eclipse做过安卓开发,现在想要把Eclipse中的项目导入到Android Studio的环境中,那么首先要做的是生成Build Gradle的文件.因为Android Studi ...