UVa1628 UVaLive5847 Pizza Delivery

填坑系列(p.302)

既然不知道后面还要卖多少个就加一维状态嘛。。

lrj写的O(n)转移?其实转移可以O(1)

貌似按x排序有奇效?

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<algorithm>
 5 #include<iostream>
 6 
 7 using namespace std;
 8 
 9 void setIO(const string& s) {
10     freopen((s + ".in").c_str(), "r", stdin);
11     freopen((s + ".out").c_str(), "w", stdout);
12 }
13 template<typename Q> Q read(Q& x) {
14     static char c, f;
15     for(f = 0; c = getchar(), !isdigit(c); ) if(c == '-') f = 1;
16     for(x = 0; isdigit(c); c = getchar()) x = x * 10 + c - '0';
17     if(f) x = -x;
18     return x;
19 }
20 template<typename Q> Q read() {
21     static Q x; read(x); return x;
22 }
23 
24 const int N = 100 + 10, INF = 0x3f3f3f3f;
25 
26 int p[N], v[N], n, cas;
27 int f[N][N][N][2];
28 int vis[N][N][N][2];
29 
30 int dfs(int s, int e, int cnt, int pos) {
31     if(!cnt) return 0;
32     int &ans = f[s][e][cnt][pos];
33     if(vis[s][e][cnt][pos] == cas) return ans;
34     vis[s][e][cnt][pos] = cas;
35     
36     ans = -INF;
37     int a[] = {s, e};
38     
39     /*for(int i = 0; i < s; i++) {
40         ans = max(ans, v[i] - cnt * abs(p[i] - p[a[pos]]) + dfs(i, e, cnt - 1, 0));
41     }
42     for(int i = e + 1; i < n; i++) {
43         ans = max(ans, v[i] - cnt * abs(p[i] - p[a[pos]]) + dfs(s, i, cnt - 1, 1));
44     }*/
45     
46     if(s - 1 >= 0) {
47         int dlt = cnt * abs(p[s - 1] - p[a[pos]]);
48         ans = max(ans, v[s - 1] - dlt + dfs(s - 1, e, cnt - 1, 0));
49         ans = max(ans, -dlt + dfs(s - 1, e, cnt, 0));
50     }
51     if(e + 1 < n) {
52         int dlt = cnt * abs(p[e + 1] - p[a[pos]]);
53         ans = max(ans, v[e + 1] - dlt + dfs(s, e + 1, cnt - 1, 1));
54         ans = max(ans, -dlt + dfs(s, e + 1, cnt, 1)) ;
55     }
56     
57     return ans;
58 }
59 
60 int main() {
61 #ifdef DEBUG
62     freopen("in.txt", "r", stdin);
63     freopen("out.txt", "w", stdout);
64 #endif
65     
66     int T; scanf("%d", &T);
67     for(cas = 1; cas <= T; cas++) {
68         scanf("%d", &n);
69         for(int i = 0; i < n; i++) scanf("%d", p + i);
70         for(int i = 0; i < n; i++) scanf("%d", v + i);
71         
72         int ans = 0;
73         for(int k = 1; k <= n; k++) {
74             for(int i = 0; i < n; i++) {
75                 ans = max(ans, v[i] - k * abs(p[i]) + dfs(i, i, k - 1, 0));
76             }
77         }
78         printf("%d
", ans);
79     }
80     
81     return 0;
82 }
View Code
原文地址:https://www.cnblogs.com/showson/p/5101788.html