【暑假】[深入动态规划]UVa 1628 Pizza Delivery
UVa 1628 Pizza Delivery
题目:
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51189
思路:
本体与修缮长城一题有所相似。所以解法有相似之处。
不同之处就是本体可能会产生负情况,即送餐时间晚了客户会反过来找你要钱所以需要放弃,但修缮长城只有费用,顺手修了肯定是一个不错的选择。
依旧将区间两端与位置作为状态不过要添加一维cnt表示还需要送餐的人数。类似地定义:d[i][j][cnt][p]表示已经送完了ij区间(区间内或送餐或放弃)位于p(p==0||p==1)还剩下cnt个客户需要继续送餐。添加的一维成功解决了不知道还有多少的客户需要送餐的问题。这里注意到DP过程中利用的信息都来源于状态,因此定义的状态必须要提供转移足够的信息,这样才能得到所需要的值。
转移方程:
d[i][j][cnt][p]=max{d[i][k][cnt-1][1]+pay[knew]//k在余下的右区间 , d[k][j][cnt-1][0] +pay[knew]//k在余下的左区间}
代码:
// UVa1628 Pizza Delivery
// Rujia Liu
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = + ; int kase, n;
int p[maxn], v[maxn];
int d[maxn][maxn][maxn][];
int vis[maxn][maxn][maxn][]; // already considered s~e, still need to delivery to cnt people.
// pos = 0 means at s, pos = 1 means at e
int dp(int s, int e, int cnt, int pos) {
if(cnt == ) return ; //cnt==0 return int &ans = d[s][e][cnt][pos]; //记忆化搜索
if(vis[s][e][cnt][pos] == kase) return ans;
vis[s][e][cnt][pos] = kase; ans = ;
//枚举的i与之前区间相间的部分默认为不会送餐 //比较得出max_ans
if(!pos) { //pos==s
for(int i = ; i < s; i++) //s->i //i在区间的左边
ans = max(ans, v[i] - cnt * abs(p[i] - p[s]) + dp(i, e, cnt - , ));
//ans=max(ans,足够已知的未来价值+子问题价值)
for(int i = e + ; i < n; i++) //s->i //i在区间的右边
ans = max(ans, v[i] - cnt * abs(p[i] - p[s]) + dp(s, i, cnt - , ));
}
else { //pos==e
for(int i = ; i < s; i++) //e->i //i在区间的左边
ans = max(ans, v[i] - cnt * abs(p[i] - p[e]) + dp(i, e, cnt - , ));
for(int i = e + ; i < n; i++) //e->i //i在区间的右边
ans = max(ans, v[i] - cnt * abs(p[i] - p[e]) + dp(s, i, cnt - , ));
}
return ans;
} //DP要枚举出所有具有最优可能性的情况 不能遗漏否则问题就可能不是最优 int main() {
int T;
scanf("%d",&T);
memset(vis, , sizeof(vis));
for(kase = ; kase <= T; kase++) {
scanf("%d", &n);
for(int i = ; i < n; i++) scanf("%d", &p[i]); //位置[]
for(int i = ; i < n; i++) scanf("%d", &v[i]); //原利[] int ans = ;
for(int k = ; k <= n; k++) //枚举送餐人数
for(int i = ; i < n; i++) //枚举给送餐的第一个人
ans = max(ans, v[i] - k * abs(p[i]) + dp(i, i, k - , )); //枚举比较 make_max
printf("%d\n",ans);
}
return ;
}
【暑假】[深入动态规划]UVa 1628 Pizza Delivery的更多相关文章
- Pizza Delivery
Pizza Delivery 时间限制: 2 Sec 内存限制: 128 MB 题目描述 Alyssa is a college student, living in New Tsukuba Cit ...
- 【暑假】[深入动态规划]UVa 1380 A Scheduling Problem
UVa 1380 A Scheduling Problem 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=41557 ...
- 【暑假】[深入动态规划]UVa 12170 Easy Climb
UVa 12170 Easy Climb 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=24844 思路: 引别人一 ...
- 【暑假】[深入动态规划]UVa 10618 The Bookcase
UVa 12099 The Bookcase 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42067 思路: ...
- 【暑假】[深入动态规划]UVa 10618 Fun Game
UVa 10618 Fun Game 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36035 思路: 一圈人围坐 ...
- 【暑假】[深入动态规划]UVa 10618 Fixing the Great Wall
UVa 10618 Fixing the Great Wall 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=361 ...
- 【暑假】[深入动态规划]UVa 1627 Team them up!
UVa 1627 Team them up! 题目: Team them up! Time Limit: 3000MS Memory Limit: Unknown 64bit IO Forma ...
- 【暑假】[深入动态规划]UVa 10618 Tango Tango Insurrection
UVa 10618 Tango Tango Insurrection 题目: Problem A: Tango Tango Insurrection You are attempting to lea ...
- 【暑假】[深入动态规划]UVa 1412 Fund Management
UVa 1412 Fund Management 题目: UVA - 1412 Fund Management Time Limit: 3000MS Memory Limit: Unknown ...
随机推荐
- XCode6.1中的ios7.1适配
在xcode6.1中新创建的项目,运行在我的ios7.1的ipod touch上时(与5s的一样的尺寸, Retina屏幕), 上下出现了黑边,由于没有下载7.1的模拟器,不知道模拟器上有无问题, 查 ...
- TaskTracker执行map或reduce任务的过程(二)
上次说到,当MapLauncher或ReduceLancher(用于执行任务的线程,它们扩展自TaskLauncher),从它们所维护的LinkedList也即队列中获取到TaskInProgress ...
- eclipse颜色配置
Eclipse颜色主题插件:Eclipse Color Theme http://blog.sina.com.cn/s/blog_674212810101go8x.html 一个很赞的eclipse插 ...
- POJ 3252 Round Numbers(数位dp)
题意:给定区间[l,r],l < r ,求区间中满足条件的正整数的个数:二进制表示下0的个数不少于1的个数. 分析:f(x)表示<=x时满足条件的数的个数,所求问题即为f(r)-f(l-1 ...
- VLAN是什么
VLAN是什么?VLAN,是英文Virtual Local Area Network的缩写,中文名为"虚拟局域网", VLAN是一种将局域网(LAN)设备从逻辑上划分(注意,不是从 ...
- (转)详解LVS负载均衡之三种工作模型原理和10种调度算法
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://linuxnx.blog.51cto.com/6676498/1195379 LV ...
- Windows Embedded Compact 2013升级:VS2013也能编译
IT之家(www.ithome.com):Windows Embedded Compact 2013升级:VS2013也能编译 今天,微软为Windows Embedded Compact 2013送 ...
- java学习面向对象之匿名内部类
之前我们提到“匿名”这个字眼的时候,是在学习new对象的时候,创建匿名对象的时候用到的,之所以说是匿名,是因为直接创建对象,而没有把这个对象赋值给某个值,才称之为匿名. 匿名对象回顾: class N ...
- 根据block取出页号buf_block_get_page_no
/*********************************************************************//** Gets the page number of a ...
- poj2391,poj2455
这两题本质是一致的: 一般来说,对于最长(短)化最短(长)的问题我们一般都使用二分答案+判定是否可行 因为这样的问题,我们一旦知道答案,就能知道全局信息 拿poj2455举例,对于二分出的一个答案,我 ...