【暑假】[深入动态规划]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 ...
随机推荐
- [转载]js javascript 判断字符串是否包含某字符串,String对象中查找子字符,indexOf
var Cts = "bblText"; if(Cts.indexOf("Text") > 0 ) { alert('Cts中包含Text字符串'); }
- CSRF防范策略研究
目录 0x1:检查网页的来源 0x2:检查内置的隐藏变量 0x3:用POST不用GET 检查网页的来源应该怎么做呢?首先我们应该检查$_SERVER[“HTTP_REFERER”]的值与来源网页的网址 ...
- jquery mobile validation
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...
- How To Monitor Remote Linux Host using Nagios 3.0
In the previous post Nagios 3.0 Jumpstart guide , I explained the overview, installation and configu ...
- ajax请求返回json数据弹出下载框的解决方法
将返回的Content-Type由application/json改为text/html. 在struts2下: <action name="XXXAjax" class=& ...
- iOS中关于KVC与KVO知识点
iOS中关于KVC与KVO知识点 iOS中关于KVC与KVO知识点 一.简介 KVC/KVO是观察者模式的一种实现,在Cocoa中是以被万物之源NSObject类实现的NSKeyValueCodin ...
- Fibonacci sequence 求余数
#include <iostream> using namespace std; int f(int n); int main() { int n; cin>>n; doubl ...
- Ubuntu 12.04上编译Vim7.4的时候遇到“no terminal library found”问题
错误如下: no terminal library foundchecking for tgetent()... configure: error: NOT FOUND! You need ...
- 键盘KeyCode值列表
event.keycode值大全1 keycode 8 = BackSpace BackSpace 2 keycode 9 = Tab Tab 3 keycode 12 = Clear 4 keyco ...
- C#读取注册表信息
注册表是视窗系统的一个核心的数据库,在这个数据库中存放中与系统相关的各种参数,这些参数直接控制中系统的启动.硬件的驱动程序安装信息以及在视窗系统上运行的各种应用程序的注册信息等.这就意味着,如果注册表 ...