UVa1628 UVaLive5847 Pizza Delivery
填坑系列(p.302)
既然不知道后面还要卖多少个就加一维状态嘛。。
lrj写的O(n)转移?其实转移可以O(1)
貌似按x排序有奇效?
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream> using namespace std; void setIO(const string& s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
template<typename Q> Q read(Q& x) {
static char c, f;
for(f = ; c = getchar(), !isdigit(c); ) if(c == '-') f = ;
for(x = ; isdigit(c); c = getchar()) x = x * + c - '';
if(f) x = -x;
return x;
}
template<typename Q> Q read() {
static Q x; read(x); return x;
} const int N = + , INF = 0x3f3f3f3f; int p[N], v[N], n, cas;
int f[N][N][N][];
int vis[N][N][N][]; int dfs(int s, int e, int cnt, int pos) {
if(!cnt) return ;
int &ans = f[s][e][cnt][pos];
if(vis[s][e][cnt][pos] == cas) return ans;
vis[s][e][cnt][pos] = cas; ans = -INF;
int a[] = {s, e}; /*for(int i = 0; i < s; i++) {
ans = max(ans, v[i] - cnt * abs(p[i] - p[a[pos]]) + dfs(i, e, cnt - 1, 0));
}
for(int i = e + 1; i < n; i++) {
ans = max(ans, v[i] - cnt * abs(p[i] - p[a[pos]]) + dfs(s, i, cnt - 1, 1));
}*/ if(s - >= ) {
int dlt = cnt * abs(p[s - ] - p[a[pos]]);
ans = max(ans, v[s - ] - dlt + dfs(s - , e, cnt - , ));
ans = max(ans, -dlt + dfs(s - , e, cnt, ));
}
if(e + < n) {
int dlt = cnt * abs(p[e + ] - p[a[pos]]);
ans = max(ans, v[e + ] - dlt + dfs(s, e + , cnt - , ));
ans = max(ans, -dlt + dfs(s, e + , cnt, )) ;
} return ans;
} int main() {
#ifdef DEBUG
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif int T; scanf("%d", &T);
for(cas = ; cas <= T; cas++) {
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]) + dfs(i, i, k - , ));
}
}
printf("%d\n", ans);
} return ;
}
UVa1628 UVaLive5847 Pizza Delivery的更多相关文章
- 【暑假】[深入动态规划]UVa 1628 Pizza Delivery
UVa 1628 Pizza Delivery 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51189 思路: ...
- Pizza Delivery
Pizza Delivery 时间限制: 2 Sec 内存限制: 128 MB 题目描述 Alyssa is a college student, living in New Tsukuba Cit ...
- uva1628 Pizza Delivery
fixing great wall 的变形dp(i,j,k,p)不考虑i-j的客人,还要送k个人,目前位置在p起点i和总数量k都要枚举dp(i,j,k,p)=max(dp(m,j,k-1,p)+val ...
- Aizu - 1383 Pizza Delivery (最短路图+DAG上的割边)
题意:给出一张有向图,每条边有长度,对于每条边,你要回答将该边的方向取反后,从起点到终点的最短距离是增加or减小or不变. 首先求出起点到所有点的最短距离和所有点到终点的最短距离(两次DIjkstra ...
- (好题)2017-2018 ACM-ICPC, Asia Tsukuba Regional Contest F Pizza Delivery
题意:给n个点m条边的有向图.每次使一条边反向,问你1到2的最短路变短,变长,还是不变. 解法:遇到这种题容易想到正向求一遍最短路d1,反向再求一遍最短路d2.纪录原图上的最短路为ans,然后分开考虑 ...
- [GodLove]Wine93 Tarining Round #1
比赛链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=44664#overview 题目来源: 2011 Asia Regional ...
- CodeForces 151B Phone Numbers
Phone Numbers Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Sub ...
- Asia-Tsukuba 2017
A. Secret of Chocolate Poles DP,$f[i][j]$表示高度为$i$,顶层颜色为$j$的方案数. 时间复杂度$O(l)$. #include<cstdio> ...
- 2017-2018 ACM-ICPC, Asia Tsukuba Regional Contest
2017-2018 ACM-ICPC, Asia Tsukuba Regional Contest A Secret of Chocolate Poles 思路:暴力枚举黑巧克力的个数和厚黑巧克力的个 ...
随机推荐
- Fast Report Data Filter
使用Data Filter两种方式:一种是 直接在Filter 属性里写表达式 ,另外一种就是在beforePrint 事件里写方法. 今天开发时遇到了一个Filter的问题,不知道是不是fast r ...
- linux服务端的网络编程
常见的Linux服务端的开发模型有多进程.多线程和IO复用,即select.poll和epoll三种方式,其中现在广泛使用的IO模型主要epoll,关于该模型的性能相较于select和poll要好不少 ...
- CentOS 6.4 64位 搭建MySQL-Cluster 7.3.8 集群
准备:1.下载 MySQL-Cluster-gpl-7.3.8-1.el6.x86_64.rpm-bundle.tar 下载地址: http://mirrors.sohu.com/mysql/MySQ ...
- JavaScript学习总结【2】、JS基础
1.JS 命名规范 命名规范是很有必要的,可增强代码的可读性,一眼就能看懂要表达的意思,规范就是符合规则,使代码有利于后期维护,也能很大程度的提高开发效率.一个正常的网站有很多 JS 代码,如果在编写 ...
- Android艺术开发探索第四章——View的工作原理(上)
这章就比较好玩了,主要介绍一下View的工作原理,还有自定义View的实现方法,在Android中,View是一个很重要的角色,简单来说,View是Android中视觉的呈现,在界面上Android提 ...
- 复制档案或目录 linux cp命令详解
cp (复制档案或目录) [root@linux ~]# cp [-adfilprsu] 来源档(source) 目的檔(destination)[root@linux ~]# cp [options ...
- sql数据库优化技巧汇总
(转)SQL 优化原则 一.问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用系统提交实际应用后,随着 ...
- 编程思想—控制反转(IOC)及依赖注入(DI)
1.什么是依赖注入 在面向对象的编程语言中,一个对象的行为方法往往需要外界的对象的行为协助才能完成. 例如:小李去ATM机取钱,那小李的取钱的整个行为的完成需要ATM实例取款行为的协助才能完成. pu ...
- MySQL中group_concat函数,用符号连接查询分组里字段值
http://blog.csdn.net/my_yang/article/details/7186844
- 【uva10917】Walk Through the Forest (最短路)
题目: gbn最近打算穿过一个森林,但是他比较傲娇,于是他决定只走一些特殊的道路,他打算只沿着满足如下条件的(A,B)道路走:存在一条从B出发回家的路,比所有从A出发回家的路径都短.你的任务是计算一共 ...