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. 1053: [HAOI2007]反素数ant - BZOJ

    Description 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4. 如果某个正整数x满足:g(x)>g(i) 0<i<x,则称x为反质数.例如,整数 ...

  2. 1034: [ZJOI2008]泡泡堂BNB - BZOJ

    Description 第XXXX届NOI期间,为了加强各省选手之间的交流,组委会决定组织一场省际电子竞技大赛,每一个省的代表队由n名选手组成,比赛的项目是老少咸宜的网络游戏泡泡堂.每一场比赛前,对阵 ...

  3. BeanFactory和FactoryBean

    BeanFactory和FactoryBean 1.BeanFactory BeanFactory定义了 IOC 容器的最基本形式,并提供了 IOC 容器应遵守的的最基本的接口,也就是Spring I ...

  4. spoj 39

    DP  完全背包问题 的   d[i] = min(d[i], d[i-w]+p)   d[i]表示当总重量为i时可装的最小价值 #include <cstdio> #include &l ...

  5. Spark的TorrentBroadcast:实现

    依据Spark 1.4版 序列化和反序列化 前边提到,TorrentBroadcast的关键就在于特殊的序列化和反序列化设置.1.1版的TorrentBroadcast实现了自己的readObject ...

  6. SQL注入file导入常用手段

    在注入过程中,如果存在注入点,可以直接导入一句话或者上传页面.过程中我们主要是利用into outfile函数进行上传.此处介绍两种关于into outfile利用的方式. 第一种直接将select内 ...

  7. poj 3301 Texas Trip 三分法

    思路:三分法求解凸函数的极值,三分法介绍在这:http://hi.baidu.com/czyuan_acm/item/81b21d1910ea729c99ce33db 很容易就可以推出旋转后的坐标: ...

  8. ASP.net 判断上传文件类型的三种方法

    一. 安全性比较低,把文本文件1.txt改成1.jpg照样可以上传,但其实现方法容易理解,实现也简单,所以网上很多还是采取这种方法. Boolean fileOk = false; string pa ...

  9. UIActinSheet和UIActionSheetDelegate

    UIActinSheet和UIActionSheetDelegate 这个是就那个UIActionSheet对象  一般用来选择类型或者改变界面...还有更多应用 定义如下:UIActionSheet ...

  10. 组策略限制添加用户作为服务登录导致ITAtomcat服务无法启动(log on as a service)

    [故障类型]:ITA tomcat服务器无法启动. [关 键 词]:Logon as a service  作为服务登录  tomcat  loggeter [适用版本]:FusionCloud So ...