HDU 4719 Oh My Holy FFF(DP+线段树)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

Description

N soldiers from the famous "*FFF* army" is standing in a line, from left to right.  
 o   o   o   o   o   o   o   o   o   o   o   o   o   o   o   o   o   o /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \
You, as the captain of *FFF*, want to divide them into smaller groups, but each group should still be continous in the original line. Like this:  
 o   o   o  |  o   o   o   o  |  o   o   o   o   o   o  |  o   o   o   o   o /F\ /F\ /F\ | /F\ /F\ /F\ /F\ | /F\ /F\ /F\ /F\ /F\ /F\ | /F\ /F\ /F\ /F\ /F\ / \ / \ / \ | / \ / \ / \ / \ | / \ / \ / \ / \ / \ / \ | / \ / \ / \ / \ / \
In your opinion, the number of soldiers in each group should be no more than L.   Meanwhile, you want your division be "holy". Since the soldier may have different heights, you decide that for each group except the first one, its last soldier(which is the rightmost one) should be strictly taller than the previous group's last soldier. That is, if we set bi as the height of the last soldier in group i. Then for i >= 2, there should be b   i  > b   i-1.   You give your division a score, which is calculated as   , b   0  = 0 and 1 <= k <= M, if there are M groups in total. Note that M can equal to 1.   Given the heights of all soldiers, please tell us the best score you can get, or declare the division as impossible.
 

Input

The first line has a number T (T <= 10) , indicating the number of test cases.   For each test case, first line has two numbers N and L (1 <= L <= N <= 10   5), as described above.   Then comes a single line with N numbers, from H1 to Hn, they are the height of each soldier in the line, from left to right. (1 <= H   i  <= 10   5)
 

Output

For test case X, output "Case #X: " first, then output the best score.

 

题目大意:有n个数,划分为多个部分,假设M份,每份不能多于L个。每个数有一个h[i],每份最右边的那个数要大于前一份最右边的那个数。设每份最右边的数为b[i],求最大的sum{b[i]² - b[i - 1]},1≤i≤M,其中b[0] = 0。

思路:朴素DP为,dp[i]表示以i为结尾的最大划分。那么dp[i] = max{dp[j] - h[j] + h[i]²},1≤i-j≤L,h[j]<h[i]。这种会超时,采取线段树优化。因为有两个限制,考虑到若h[j]≥h[i],那么求i的时候一定不会用到j,那么先按h排序再DP(h相同的,i大的排前面)。

PS:又忘了把int改成long long >_<

 

代码(781MS):

 1 #include <cstdio>

 2 #include <iostream>

 3 #include <cstring>

 4 #include <algorithm>

 5 using namespace std;

 6 typedef long long LL;

 7 

 8 const int MAXN = 100010;

 9 

10 LL dp[MAXN];

11 int n, L;

12 LL tree[MAXN << 2], maxt[MAXN << 2];

13 

14 void pushdown(int x) {

15     int ll = x << 1, rr = ll ^ 1;

16     if(tree[x] != -1) {

17         tree[ll] = max(tree[x], tree[ll]);

18         tree[rr] = max(tree[x], tree[rr]);

19         maxt[ll] = max(maxt[ll], tree[x]);

20         maxt[rr] = max(maxt[rr], tree[x]);

21         tree[x] = -1;

22     }

23 }

24 

25 void update(int x, int left, int right, int a, int b, LL val) {

26     if(a <= left && right <= b) {

27         tree[x] = max(tree[x], val);

28         maxt[x] = max(maxt[x], val);

29     }

30     else {

31         pushdown(x);

32         int ll = x << 1, rr = ll ^ 1;

33         int mid = (left + right) >> 1;

34         if(a <= mid) update(ll, left, mid, a, b, val);

35         if(mid < b) update(rr, mid + 1, right, a, b, val);

36         maxt[x] = max(maxt[x], max(maxt[ll], maxt[rr]));

37     }

38 }

39 

40 LL query(int x, int left, int right, int a, int b) {

41     if(a <= left && right <= b) return maxt[x];

42     else {

43         pushdown(x);

44         int ll = x << 1, rr = ll ^ 1;

45         int mid = (left + right) >> 1;

46         LL ret = -1;

47         if(a <= mid) ret = max(ret, query(ll, left, mid, a, b));

48         if(mid < b) ret = max(ret, query(rr, mid + 1, right, a, b));

49         return ret;

50     }

51 }

52 

53 struct Node {

54     int h, pos;

55     void read(int i) {

56         pos = i;

57         scanf("%d", &h);

58     }

59     bool operator < (const Node &rhs) const {

60         if(h != rhs.h) return h < rhs.h;

61         return pos > rhs.pos;

62     }

63 } a[MAXN];

64 

65 LL solve() {

66     sort(a + 1, a + n + 1);

67     dp[n] = -1;

68     memset(tree, 255, sizeof(tree));

69     memset(maxt, 255, sizeof(maxt));

70     update(1, 0, n, 0, 0, 0);

71     for(int i = 1; i <= n; ++i) {

72         LL tmp = query(1, 0, n, max(0, a[i].pos - L), a[i].pos - 1);

73         if(tmp == -1) {

74             if(a[i].pos == n) break;

75             else continue;

76         }

77         dp[a[i].pos] = tmp + LL(a[i].h) * a[i].h;

78         if(a[i].pos == n) break;

79         update(1, 0, n, a[i].pos, a[i].pos, dp[a[i].pos] - a[i].h);

80     }

81     //for(int i = 1; i <= n; ++i) printf("%I64d\n", dp[i]);

82     return dp[n];

83 }

84 

85 int main() {

86     int T; scanf("%d", &T);

87     for(int t = 1; t <= T; ++t) {

88         scanf("%d%d", &n, &L);

89         for(int i = 1; i <= n; ++i) a[i].read(i);

90         LL ans = solve();

91         if(ans == -1) printf("Case #%d: No solution\n", t);

92         else printf("Case #%d: %I64d\n", t, ans);

93     }

94 }
View Code

 

你可能感兴趣的:(online)