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的更多相关文章

  1. Pizza Delivery

    Pizza Delivery 时间限制: 2 Sec  内存限制: 128 MB 题目描述 Alyssa is a college student, living in New Tsukuba Cit ...

  2. 【暑假】[深入动态规划]UVa 1380 A Scheduling Problem

     UVa 1380 A Scheduling Problem 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=41557 ...

  3. 【暑假】[深入动态规划]UVa 12170 Easy Climb

    UVa 12170 Easy Climb 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=24844 思路:  引别人一 ...

  4. 【暑假】[深入动态规划]UVa 10618 The Bookcase

    UVa 12099  The Bookcase 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42067 思路:    ...

  5. 【暑假】[深入动态规划]UVa 10618 Fun Game

    UVa 10618 Fun Game 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36035 思路:   一圈人围坐 ...

  6. 【暑假】[深入动态规划]UVa 10618 Fixing the Great Wall

    UVa 10618 Fixing the Great Wall 题目:  http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=361 ...

  7. 【暑假】[深入动态规划]UVa 1627 Team them up!

    UVa 1627 Team them up! 题目: Team them up! Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Forma ...

  8. 【暑假】[深入动态规划]UVa 10618 Tango Tango Insurrection

    UVa 10618 Tango Tango Insurrection 题目: Problem A: Tango Tango Insurrection You are attempting to lea ...

  9. 【暑假】[深入动态规划]UVa 1412 Fund Management

    UVa 1412 Fund Management 题目: UVA - 1412 Fund Management Time Limit: 3000MS   Memory Limit: Unknown   ...

随机推荐

  1. 基于注解风格的Spring-MVC的拦截器

    基于注解风格的Spring-MVC的拦截器 Spring-MVC如何使用拦截器,官方文档只给出了非注解风格的例子.那么基于注解风格如何使用拦截器呢? 基于注解基本上有2个可使用的定义类,分别是Defa ...

  2. XSD - <schema> 元素

    <schema> 元素 <schema> 元素是每一个 XML Schema 的根元素: <?xml version="1.0"?> <x ...

  3. wamp设置实现本机IP或者局域网访问 (转)

    <Directory /> Options FollowSymLinks AllowOverride None Order deny,allow Allow from all #以前是De ...

  4. PLSQL显示乱码-无法进行中文条件查询解决

    PLSQL显示乱码-无法进行中文条件查询解决 原因: PLSQL乱码问题皆是ORACLE服务端字符集编码与PLSQL端字符集编码不一致引起.类似乱码问题都可以从编码是否一致上面去考虑. 解决: 1. ...

  5. OD之常用命令

    一:od断点注释保存的问题,由于od只有在正常退出的情况下才会保存分析代码时留下的注释,而很多时候为了在退出od时不让目标程序退出使用了剥离进程,这样就会导致这次操作所有的注释都没有保存,第二次重新载 ...

  6. Android中SQLite应用详解(转)

    上次我向大家介绍了SQLite的基本信息和使用过程,相信朋友们对SQLite已经有所了解了,那今天呢,我就和大家分享一下在Android中如何使用SQLite. 现在的主流移动设备像Android.i ...

  7. linq集合内部赋值

    linq集合内部赋值 比如将一个列的值,赋值给另一列 有三种方法: 1. e.Result.ToList().ForEach(n => n.IsIntermediarybool = SetIsI ...

  8. AppStore 沙箱环境的测试流程

    1:请确保你打得版本是 沙箱环境的版本 2:请确保的手机序列号已经加入沙箱环境3:请确保你的手机Apple ID 账户已经退出 ------ 如果这些都准备好了,再进行测试吧,不然一堆的未知问题等着你 ...

  9. Android:控件布局(绝对布局)AbsoluteLayout

    绝对布局也叫坐标布局,指定元素的绝对位置,因为适应性很差,一般很少用到.可以使用RelativeLayout替代. 常用属性: android:layout_x  --------组件x坐标 andr ...

  10. 省市区 Mysql 数据库表

    1.查省SELECT * FROM china WHERE china.Pid=02.查市SELECT * FROM chinaWHERE china.Pid=3300003.查区SELECT * F ...