[BZOJ1563][NOI2009]诗人小G[决策单调性优化]

int T, n, m, l, p, pre[MAXN], q[MAXN], h, t, b[MAXN];//b[i]表示决策点是i的最后一个位置
llf dp[MAXN];
char s1[35];
 
inline llf get(int i, int j) {
  return dp[j] + Pow(abs(pre[i] - pre[j] + i - j - 1 - m), p);
}
 
inline int get_pos(int x, int y) {//计算x是决策点的最后一个位置
  int l = y+1, r = n, ans = 0;
  while (l <= r) 
    if (get(mid, y) > get(mid, x)) ans = mid, l = mid+1;
    else r = mid-1;
  return ans;
}
 
int main() {
#ifdef LOCAL_DEBUG
  // freopen("data.in", "r", stdin);
  Dbg = 1; uint tim1 = clock();
#endif
  in, T;
  while (T--) {
    h = 1, t = 0;
    q[++t] = 0;
    in, n, m, p;
    lop(i,1,n) {
      in, s1;
      pre[i] = strlen(s1) + pre[i - 1];
    }
    lop(i,1,n) {
      while (h < t && b[q[h]] < i) ++h;
      dp[i] = get(i, q[h]);
      while (h < t && b[q[t - 1]] >= get_pos(q[t], i)) --t;
      b[q[t]] = get_pos(q[t], i), q[++t] = i;
    }
    if (dp[n] > infl) puts("Too hard to arrange");
    else out, (ll)dp[n], '\n';
    puts("--------------------");
  }
#ifdef LOCAL_DEBUG
  fprintf(stderr, "\ntime:%.5lfms", (clock() - tim1) / (1.0 * CLOCKS_PER_SEC) * 1000);
#endif
  return 0;
}
posted @ 2018-12-28 15:28  QvvQ  阅读(171)  评论(0编辑  收藏  举报