1,多重背包的2k 拆分优化
例题:Dividing
http://poj.org/problem?id=1014
很容易看成一个多重背包模型。一个显然的做法是直接将问题看成01背包,然而复杂度无法接受。所谓
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int num[7];
int dp[100001];
int tmp[100001];
int k = 1, tp = 0;
int main() {
while (scanf("%d%d%d%d%d%d", &num[1], &num[2], &num[3], &num[4], &num[5], &num[6])){
memset(dp,0,sizeof dp);
if (num[1] == 0 && num[2]==0 && num[3]==0&&num[4]==0&&num[5]==0&&num[6]==0)
break;
printf("Collection #%d:
", k++);
int sigma = 0;
for (int i = 1; i <= 6; i++)
sigma += num[i]*i;
if (sigma&1) {
puts("Can't be divided.
");
} else {
for (int i = 1; i <= 6; i++) {
tp = 0;
for (int j = 1; num[i] >= j; j<<=1) {
if (num[i] >= j) {
tmp[++tp] = j;
num[i]-=j;
}
}
if (num[i] != 0) tmp[++tp] = num[i];
dp[0] = 1;
for (int j = 1; j <= tp; j++)
for (int k = sigma; k >= i*tmp[j]; k--)
dp[k] += dp[k-i*tmp[j]];
}
if (dp[sigma/2])
puts("Can be divided.
");
else
puts("Can't be divided.
");
}
}
return 0;
}
2. 缩小决策序列
例题1:Railway tickets
http://poj.org/problem?id=2355
这个题很容易想到1D/1D方程。显然:如果能用同样的票价走到更远的地方,何乐而不为呢?所以转移的条件是:距离这一站用每种票能走到最远的一站。由于同种票所能走到的距离是一定的,当dp中所在位置单调时,决策位置也必然单调。因此可以用摊还O(1)的时间找到决策区域,变成了1D/0D方程。不难写出程序:
注意正反分开讨论。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
long long l1,l2,l3,c1,c2,c3;
long long n, a, b;
long long f[10005];
long long dp[10005];
int main() {
//freopen("rail.in","r",stdin);
//freopen("rail.out","w",stdout);
scanf("%d%d%d%d%d%d%d%d%d", &l1, &l2, &l3, &c1, &c2, &c3, &n, &a, &b);
f[1] = 0;
for (int i = 2; i <= n; i++)
scanf("%d", &f[i]);
memset(dp, 127, sizeof dp);
if (a <= b) {
int i, j, k1,k2,k3;
k1 = k2 = k3 = a;
dp[a] = 0;
for (i = a+1; i <= b; i++) {
for (; f[i]-f[k3] > l3; k3++);
for (; f[i]-f[k2] > l2; k2++);
for (; f[i]-f[k1] > l1; k1++);
dp[i] = min(dp[k1]+c1, min(dp[k2]+c2, dp[k3]+c3));
}
cout << dp[b] << endl;
} else {
int i, j, k1,k2,k3;
k1 = k2 = k3 = a;
dp[a] = 0;
for (i = a-1; i >= b; i--) {
for (; f[k3]-f[i] > l3&&k3>=i; k3--);
for (; f[k2]-f[i] > l2&&k2>=i; k2--);
for (; f[k1]-f[i] > l1&&k1>=i; k1--);
dp[i] = min(dp[k1]+c1, min(dp[k2]+c2, dp[k3]+c3));
}
cout << dp[b] << endl;
}
return 0;
}
例题2:石子合并(经典题)
方程:
一个简单易行的方法是:用大数据测试优化是否正确。
例题3:oj 9285:盒子与小球之三
http://noi.openjudge.cn/ch0206/9285/
一道很典型的dp优化题目。
先变原问题为:从[0,k]中取出m个自然数,使其和为n
dp[i][j]表示[0,k]中取出i个,使其和为j的方案数。很容易写2d/1d方程(和子集合很像):
变形一下:
(1)-(2),并移项合并得到
至此已经转化为了
- 可以先处理第一行和第一列,可以省去很多细节判断;
- 为了防止j-k-1超界,要加一句if (j-k-1 >= 0;
- 负数取模的方法是取模 加模 再取模
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n,m,k;
int dp[5005][5005];
inline int mod(int x){
if (x > 0) return x%1000007;
return (x%1000007+1000007)%1000007;
}
int main() {
scanf("%d%d%d", &n, &m, &k);
memset(dp,0,sizeof dp);
for (int j = 1; j <= k && j <= n; j++)
dp[1][j] = 1;
for (int j = 0; j <= m; j++)
dp[j][0] = 1;
for (int i = 2; i <= m; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j] = dp[i][j-1]+dp[i-1][j];
if (j-k-1 >= 0)
dp[i][j] -= dp[i-1][j-k-1];
dp[i][j] = mod(dp[i][j]);
}
}
cout << mod(dp[m][n]) << endl;
return 0;
}
例题4:花店橱窗
http://tyvj.cn/p/1124
和3很像,且更简单,略。
#include <iostream>
#include <cstring>
using namespace std;
int n, m;
int tab[105][105];
int dp[105][105];
int choose[105];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> tab[i][j];
memset(dp,-127,sizeof dp);
dp[n][m] = tab[n][m];
for (int j = m-1; j >= 1; j--)
dp[n][j] = max(tab[n][j], dp[n][j+1]);
for (int i = n-1; i >= 1; i--)
for (int j = m-1; j >= 1; j--)
dp[i][j] = max(dp[i][j+1], dp[i+1][j+1]+tab[i][j]);
cout << dp[1][1] << endl;
for (int i = 1; i <= n; i++)
for (int j = choose[i-1]+1; j <= m; j++)
if (dp[i][j] == dp[i+1][j+1]+tab[i][j] || (i == n && dp[i][j] == tab[i][j])) {
choose[i] = j;
break;
}
for (int i = 1; i <= n; i++)
cout << choose[i] << " ";
return 0;
}
3. 乱入的一道图论题
ROADS
http://poj.org/problem?id=1724
思路来自《road结题报告,曹利国》。首先查找不计钱的最短路径,再查找不计路程的最小钱。拿这两个东西做剪枝条件,可以几乎秒杀。(是否可以证明算法是多项式的?)