洛谷P2858题解

这是一道裸的第二类区间DP(由已知区间向外扩展)题。
首先定义 \(f_{i,j}\) 为最后 \(j-i+1\) 个数取 \([i,j]\) 这个区间时,\([i,j]\) 这个区间可以产生的最大价值。那么根据定义,\(f_{i,i}=n*a_i\)
然后我们枚举区间长度长度,这样保证长度为 \(k+1\) 的区间可以被长度为 \(k\) 的区间向左右扩展得出。
容易写出向外扩展的状态转移方程是 \(f_{i,j}=\text{max}(f_{i+1,j}+(n-k+1)*a_i ,f_{i,j-1}+(n-k+1)*a_j)\)
没了。
代码:

#include<stdio.h>
#define reg register
#define ri reg int
#define rep(i, x, y) for(ri i = x; i <= y; ++i)
#define nrep(i, x, y) for(ri i = x; i >= y; --i)
#define DEBUG 1
#define ll long long
#define il inline
#define swap(a, b) ((a) ^= (b) ^= (a) ^= (b))
#define max(i, j) (i) > (j) ? (i) : (j)
#define min(i, j) (i) < (j) ? (i) : (j)
#define read(i) io.READ(i)
#define print(i) io.WRITE(i)
#define push(i) io.PUSH(i)
struct IO {
#define MAXSIZE (1 << 20)
#define isdigit(x) (x >= '0' && x <= '9')
  char buf[MAXSIZE], *p1, *p2;
  char pbuf[MAXSIZE], *pp;
#if DEBUG
#else
  IO() : p1(buf), p2(buf), pp(pbuf) {}
  ~IO() {
    fwrite(pbuf, 1, pp - pbuf, stdout);
  }
#endif
  inline char gc() {
#if DEBUG
    return getchar();
#endif
    if(p1 == p2)
      p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
    return p1 == p2 ? ' ' : *p1++;
  }
  inline bool blank(char ch) {
    return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
  }
  template <class T>
  inline void READ(T &x) {
    register double tmp = 1;
    register bool sign = 0;
    x = 0;
    register char ch = gc();
    for(; !isdigit(ch); ch = gc())
      if(ch == '-') sign = 1;
    for(; isdigit(ch); ch = gc())
      x = x * 10 + (ch - '0');
    if(ch == '.')
      for(ch = gc(); isdigit(ch); ch = gc())
        tmp /= 10.0, x += tmp * (ch - '0');
    if(sign) x = -x;
  }
  inline void READ(char *s) {
    register char ch = gc();
    for(; blank(ch); ch = gc());
    for(; !blank(ch); ch = gc())
      *s++ = ch;
    *s = 0;
  }
  inline void READ(char &c) {
    for(c = gc(); blank(c); c = gc());
  }
  inline void PUSH(const char &c) {
#if DEBUG
    putchar(c);
#else
    if(pp - pbuf == MAXSIZE) {
      fwrite(pbuf, 1, MAXSIZE, stdout);
      pp = pbuf;
    }
    *pp++ = c;
#endif
  }
  template <class T>
  inline void WRITE(T x) {
    if(x < 0) {
      x = -x;
      PUSH('-');
    }
    static T sta[35];
    T top = 0;
    do {
      sta[top++] = x % 10;
      x /= 10;
    } while(x);
    while(top)
      PUSH(sta[--top] + '0');
  }
  template <class T>
  inline void WRITE(T x, char lastChar) {
    WRITE(x);
    PUSH(lastChar);
  }
} io;
int n, a[2010], f[2010][2010];
int main(){
  read(n);
  rep(i, 1, n) read(a[i]), f[i][i] = a[i] * n;
  rep(k, 1, n) {
	  rep(i, 1, n - k + 1) {
		  ri j = i + k - 1;
		  f[i][j] = max(f[i + 1][j] + (n - k + 1) * a[i], f[i][j - 1] + (n - k + 1) * a[j]);
		}
	}
	print(f[1][n]);
	return 0;
} 
posted @ 2021-03-27 08:16  1358id  阅读(121)  评论(0编辑  收藏  举报