UVa1628

By reading the title, we find this question very like the construction of the Great Wall, built the Great Wall and easily repair some better, but feels that she pizza sent in this question can not be better, could

"Subsidizing", so as not completely solve the problem as the construction of the Great Wall, then this question how to do it?

And from reading the title, we found very little data range, and the same for each customer's cost per unit of time, which tells us that the state can increase the number of dimensions or more decisions to

This question is close, because we can not determine which customers to send pizza, regardless of the customer, it must be reflected on the state.

We set d (i, j, k, p) represents [i, j] regardless of the interval (this interval may or may not have sent a pizza) but also to the user k maximum benefit out of pizza, where p is 0 description i Department,

p is 1 j description, the state transition equation of the line to see the program, the time in which the method of seeking the very clever, Here is the code:

// UVa 1628
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std; 

const int maxn = 100 + 5; 

int n, pos[maxn], v[maxn], kase;  
int d[maxn][maxn][maxn][2], vis[maxn][maxn][maxn][2]; 

int dp(int s, int e, int k, int p) {
  if (k == 0) return 0; 
  
  int& ans = d[s][e][k][p];
  if (vis[s][e][k][p] == kase) return ans; 
  vis[s][e][k][p] = kase;     
  
  ans = 0; 
  if (!p) {
    for (int i = 0; i < s; ++i) 
      ans = max(ans, v[i]-k*abs(pos[i]-pos[s])+dp(i,e,k-1,0));
    for (int i = e+1; i < n; ++i)    
      ans = max(ans, v[i]-k*abs(pos[i]-pos[s])+dp(s,i,k-1,1)); 
  } 
  else {
    for (int i = 0; i < s; ++i) 
      ans = max(ans, v[i]-k*abs(pos[i]-pos[e])+dp(i,e,k-1,0)); 
    for (int i = e+1; i < n; ++i) 
      ans = max(ans, v[i]-k*abs(pos[i]-pos[e])+dp(s,i,k-1,1));  
  }
  return ans; 
}

int main() { 
  int t; 
  scanf("%d", &t); 
  for (kase = 1; kase <= t; ++kase) {
    scanf("%d", &n); 
    for (int i = 0; i < n; ++i) scanf("%d", &pos[i]); 
    for (int i = 0; i < n; ++i) scanf("%d", &v[i]); 
    
    int ans = 0; 
    for (int k = 1; k <= n; ++k) 
      for (int i = 0; i < n; ++i) 
        ans = max(ans, v[i]-k*abs(pos[i])+dp(i,i,k-1,0));
    printf("%d\n", ans); 
  }
  return 0;
}

 

Guess you like

Origin www.cnblogs.com/yifeiWa/p/11320202.html