BZOJ1563: [NOI2009]诗人小G(决策单调性 前缀和 dp)

题意

题目链接

Sol

很显然的一个dp方程

\(f_i = min(f_j + (sum_i - sum_j - 1 - L)^P)\)

其中\(sum_i = \sum_{j = 1}^i len_j + 1\)

这个东西显然是有决策单调性的。

单调队列优化一下

我好像已经做过三个这种类型的题了,而且转移的时候\(w\)中总是带个幂函数。。interesting

#include<bits/stdc++.h>
#define chmax(a, b) (a = (a > b ? a : b))
#define chmin(a, b) (a = (a < b ? a : b))
#define LL long long
#define LDB long double 
//#define int long long 
using namespace std;
const int MAXN = 1e5 + 10;
inline int read() {
    int x = 0, f = 1; char c = getchar();
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int T, N, L, P, sum[MAXN], q[MAXN], c[MAXN], pre[MAXN];//c???ߵ?λ?
char str[MAXN][35];
LDB f[MAXN];
LDB fastpow(LDB a, int p) {
	LDB base = 1;
	while(p) {
		if(p & 1) base = base * a; 
		a = a * a; p >>= 1;
	}
	return base;
}
LDB calc(int j, int i) {
	return f[j] + fastpow(abs(sum[i] - sum[j] - L), P);
}
int lower(int x, int y) {//???x????????
	int l = x, r = N + 1, ans = 0;
	while(l <= r) {
		int mid = l + r >> 1;
		if(calc(x, mid) >= calc(y, mid)) r = mid - 1;
		else l = mid + 1;
	}
	return l;
}
void solve() {
	N = read(); L = read() + 1; P = read();
	for(int i = 1; i <= N; i++) {
		scanf("%s", str[i] + 1);
		sum[i] = sum[i - 1] + strlen(str[i] + 1) + 1;
	}
	memset(q, 0, sizeof(q));
	for(int i = 1, h = 2, t = 2; i <= N; i++) {
		while(h < t && c[h] <= i) h++;
		f[i] = calc(q[h], i); pre[i] = q[h];
		while(h < t && c[t - 1] >= lower(q[t], i)) t--;
		c[t] = lower(q[t], i); q[++t] = i;
	}
	if(f[N] > 1e18) {puts("Too hard to arrange\n--------------------"); return;}
	printf("%.0Lf\n", f[N]);
    puts("--------------------");
}
main() {
	for(T = read(); T; T--) solve();
}

posted @ 2018-10-09 21:27  自为风月马前卒  阅读(319)  评论(0编辑  收藏  举报

Contact with me