zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    经济危机下,自主创业已成为一种时代潮流?
    曾靖雯(帮别人名字作诗)
    四大内伤造就80后创业高失败率
    如何让创业的路不再崎岖
    叶莉(帮别人名字作诗)
    谁能够在萧条中生存?历史总是惊人相似的(转)
    哪种书最适合创业者阅读
    中西部崛起 关键是要激发普遍的创业热情
    每日英语:Dating in China Is a Largely Commercial Transaction
    每日英语:Poor Chinese Schools Tell Students: Bring Your Own Desks
  • 原文地址:https://www.cnblogs.com/showson/p/5101788.html
Copyright © 2011-2022 走看看