填坑系列(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的更多相关文章

  1. 【暑假】[深入动态规划]UVa 1628 Pizza Delivery

    UVa 1628 Pizza Delivery 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51189 思路:    ...

  2. Pizza Delivery

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

  3. 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 ...

  4. Aizu - 1383 Pizza Delivery (最短路图+DAG上的割边)

    题意:给出一张有向图,每条边有长度,对于每条边,你要回答将该边的方向取反后,从起点到终点的最短距离是增加or减小or不变. 首先求出起点到所有点的最短距离和所有点到终点的最短距离(两次DIjkstra ...

  5. (好题)2017-2018 ACM-ICPC, Asia Tsukuba Regional Contest F Pizza Delivery

    题意:给n个点m条边的有向图.每次使一条边反向,问你1到2的最短路变短,变长,还是不变. 解法:遇到这种题容易想到正向求一遍最短路d1,反向再求一遍最短路d2.纪录原图上的最短路为ans,然后分开考虑 ...

  6. [GodLove]Wine93 Tarining Round #1

    比赛链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=44664#overview 题目来源: 2011 Asia Regional ...

  7. CodeForces 151B Phone Numbers

     Phone Numbers Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Sub ...

  8. Asia-Tsukuba 2017

    A. Secret of Chocolate Poles DP,$f[i][j]$表示高度为$i$,顶层颜色为$j$的方案数. 时间复杂度$O(l)$. #include<cstdio> ...

  9. 2017-2018 ACM-ICPC, Asia Tsukuba Regional Contest

    2017-2018 ACM-ICPC, Asia Tsukuba Regional Contest A Secret of Chocolate Poles 思路:暴力枚举黑巧克力的个数和厚黑巧克力的个 ...

随机推荐

  1. boost::bind实践2——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》

    直接代码: 代码段1: #include <iostream> #include <string> #include <boost/bind/bind.hpp> c ...

  2. HDU 题目分类

    转载自新浪博客,, http://blog.sina.com.cn/s/blog_71ded6bf0100tuya.html 基础题: 1000.1001.1004.1005.1008.1012.10 ...

  3. Unity_与android交互

    Unity调用Android代码 方法一: //using让 Local Ref 回收 using(AndroidJavaClass javaClazz = new AndroidJavaClass( ...

  4. 2、Python djang 框架下的word Excel TXT Image 等文件的下载

    2.python实现文件下载 (1)方法一.直接用a标签的href+数据库中文件地址,即可下载.缺点:word excel是直接弹框下载,对于image txt 等文件的下载方式是直接在新页面打开. ...

  5. 抓取天涯文章的蜘蛛代码,刚经过更新(因为天涯页面HTML代码变化)

    #_*_coding:utf-8-*- import urllib2 import traceback import codecs from BeautifulSoup import Beautifu ...

  6. Thrift 使用方法

  7. bootstrap datepicker时间插件显示位置不对

    bppystrap-datetimepicker.min.js中,修改如下:将原来的        if(!b(this.element)){l=l+document.body.scrollTop}改 ...

  8. A simple brute force problem.

    hdu4971:http://acm.hdu.edu.cn/showproblem.php?pid=4971 题意:给你n个项目,每完成一个项目会有一定的收益,但是为了完成某个项目,要先学会一些技能, ...

  9. Ecshop wap

    http://www.08kan.com/gwk/MzA3MDMxMzAxMw/200091492/1/c38b5937e4e819d9908fe3ae964e3dfc.html

  10. Java OAuth开发包资料

    原文地址:http://www.oschina.net/project/tag/307/oauth?lang=19&sort=time