Kattis peaktram (树状数组 DP)
2016-11-17 20:06
162 查看
题目链接:点击这里
题意:给出n个物体的高度以及将他们高度改变1单位的花费,一个观测点从最左端从0往上走,一直平视. 问至少能观测到k个物体的最小花费.因为高度比较大,所以需要将高度离散化,那么需要离散化哪些高度呢?容易发现对于每一个建筑,只要把它+-70范围内的高度离散化就好了,这样最多就是70*140种高度.然后用dp[i][j][k]表示到i物体,最大高度是j,一共看到k个物体的最小花费,转移式子就是
dp[i][j][k]=min{d.p[i−1][j′][k−1]+cost(j,hi)dp[i−1][j][k]+(j≤hi?cost(hi,j):0)(j′<j)(j′≥j)
不难发现对于下面一半可以直接转移,对于上面一半可以维护一个树状数组,总复杂度大约是O(n4lgn).
#include <cstdio> #include <iostream> #include <cstring> #include <queue> #include <cmath> #include <algorithm> #include <stack> #define Clear(x,y) memset (x,y,sizeof(x)) #define Close() ios::sync_with_stdio(0) #define Open() freopen ("more.in", "r", stdin) #define fi first #define se second #define pii pair<int, int> #define pli pair<long long, int> #define pb push_back #define mod 1000000007 template <class T> inline bool scan (T &ret) { char c; int sgn; if (c = getchar(), c == EOF) return 0; //EOF while (c != '-' && (c < '0' || c > '9') ) c = getchar(); sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return 1; } using namespace std; #define maxn 75 #define maxm maxn*maxn*2 #define INF 0x3f3f3f3f3f3f3f3f long long dp[maxn][maxm][maxn]; long long h[maxn], cost[maxn]; vector <int> num; long long c[maxn][maxn][maxm]; int n, len, sz; int lowbit (int x) { return x&(-x); } void update (int x, long long num, long long *c) { for (int i = x; i < maxm; i += lowbit (i)) { c[i] = min (c[i], num); } } long long query (int x, long long *c) { long long ans = INF; for (int i = x; i > 0; i -= lowbit (i)) { ans = min (ans, c[i]); } return ans; } void solve () { sz = num.size (); for (int i = 1; i <= sz; i++) { dp[1][i][1] = abs (h[1]-num[i-1])*cost[1]; update (i, dp[1][i][1], c[1][1]); } for (int i = 2; i <= n; i++) { for (int j = 1; j <= sz; j++) { for (int k = 1; k <= n; k++) { dp[i][j][k] = min (dp[i][j][k], dp[i-1][j][k] + (num[j-1]<=h[i] ? cost[i]*abs(num[j-1]-h[i]) : 0)); long long tmp = query (j-1, c[i-1][k-1]); dp[i][j][k] = min (dp[i][j][k], tmp+cost[i]*abs (h[i]-num[j-1])); update (j, dp[i][j][k], c[i][k]); } } } long long ans = INF; for (int i = 1; i <= sz; i++) { for (int j = len; j <= n; j++) { ans = min (ans, dp [i][j]); } } cout << ans << endl; } int main () { //Open (); Close (); num.clear (); Clear (c, INF); Clear (dp, INF); cin >> n >> len; for (int i = 1; i <= n; i++) { cin >> h[i] >> cost[i]; for (int j = 0; j <= 70; j++) { num.pb (h[i]+j); } for (int j = 1; j <= 70 && h[i]-j > 0; j++) { num.pb (h[i]-j); } } sort (num.begin (), num.end ()); num.erase (unique (num.begin (), num.end ()), num.end ()); solve (); return 0; }
相关文章推荐
- Kattis peaktram (DP)
- hdu2227 && hdu3450 【树状数组优化dp】
- hdoj 2227 Find the nondecreasing subsequences 【树状数组优化dp】
- HDU5293(SummerTrainingDay13-B Tree DP + 树状数组 + dfs序)
- hdu 5542(树状数组优化dp)
- HDU5542(树状数组优化DP)
- [数位DP] ZROI 2017 提高3 T1 树状数组
- bzoj 3594: [Scoi2014]方伯伯的玉米田 二维树状数组优化dp
- hdu 5489 Removed Interval 2015合肥网络赛 树状数组 dp 离散化/dp
- HDU 5542 The Battle of Chibi (离散化+树状数组优化DP)
- HDU 5542 The Battle of Chibi (ccpc 南阳 C)(DP 树状数组 离散化)
- BZOJ3594 树状数组优化DP
- LightOJ-1085-树状数组,离散化,dp
- [bzoj1264][AHOI2006]基因匹配Match 树状数组优化dp
- BZOJ 3688 树状数组优化DP 解题报告
- NOIP模拟题 2016.9.3 [数论] [逆序对] [树状数组] [树形dp]
- hdu4991 树状数组辅助DP
- HDU 6078 Wavel Sequence 树状数组优化DP
- hdu 3450 树状数组优化dp
- hdoj 3450 Counting Sequences 【离散化 + 树状数组优化dp】