#HDU4719#Oh My Holy FFF(DP+线段树优化)

xiaoxiao2021-02-28  76

Oh My Holy FFF

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1146    Accepted Submission(s): 322 Problem 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.   Sample Input 2 5 2 1 4 3 2 5 5 2 5 4 3 2 1   Sample Output Case #1: 31 Case #2: No solution

题意n(n < 1e5)个人排成一行,把它成若干堆,要求每一堆的长度不超过l(l < 1e5),并且每一堆的最右一个人的身高都要比前一堆的最右一个人的身高要高,对于每一种方案,它的分数是SUM(b[k]^2-b[k-1] )  b[k] 为第k堆最右一个人的身高 要求最高的分数。

注意是不能打乱顺序的

定义Dp[i]表示前i个人分成若干堆得到的最高分数,第i个人是结尾的那一个,所以很显然有状态转移:

Dp[i] = max(Dp[j] - H[j]) + H[i] ^ 2,i - l <= j <= i - 1

我写得很基础,线段树里面带了左右下标

线段树优化:

Status Accepted Time 998ms Memory 8136kB Length 2005 Lang G++ Submitted 2017-06-12 14:41:27 Shared RemoteRunId 20861352 #include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> using namespace std; typedef long long LL; const int Max = 100005; const int INF = 0x3f3f3f3f; struct node{ LL h; int p; bool operator<(const node & X)const{ if(h == X.h) return p > X.p; return h < X.h; } }P[Max]; struct tree{ int l, r; LL val; }Tr[Max << 2]; int N, L; LL Dp[Max]; void getll(LL & num){ char c; int flag = 1; num = 0LL; while((c = getchar()) < '0' || c > '9') if(c == '-') flag = -1; while(c >= '0'&& c <= '9'){ num = num * 10 + c - 48; c = getchar();} num *= flag; } void getint(int & num){ char c; int flag = 1; num = 0; while((c = getchar()) < '0' || c > '9') if(c == '-') flag = -1; while(c >= '0'&& c <= '9'){ num = num * 10 + c - 48; c = getchar();} num *= flag; } void build(int i, int l, int r){ Tr[i].l = l, Tr[i].r = r; Tr[i].val = -1; if(l == r) return ; int mid = (l + r) >> 1; build(i << 1, l, mid); build(i << 1 | 1, mid + 1, r); } void insert(int i, int p, LL val){ if(Tr[i].r < p || Tr[i].l > p) return ; if(Tr[i].l == p && Tr[i].r == p){ Tr[i].val = max(Tr[i].val, val); return ; } insert(i << 1, p, val); insert(i << 1 | 1, p, val); Tr[i].val = max(Tr[i << 1].val, Tr[i << 1 | 1].val); } LL Query(int i, int l, int r){ if(Tr[i].r < l || Tr[i].l > r) return -1; if(l <= Tr[i].l && r >= Tr[i].r) return Tr[i].val; return max(Query(i << 1, l, r), Query(i << 1 | 1, l, r)); } int main(){ int T, tot = 0; getint(T); while(T --){ getint(N), getint(L); printf("Case #%d: ", ++ tot); for(int i = 1; i <= N; ++ i) getll(P[i].h), P[i].p = i; sort(P + 1, P + N + 1); build(1, 0, N); insert(1, 0, 0); for(int p = 1; p <= N; ++ p){ int i = P[p].p; LL j = Query(1, max(i - L, 0), i - 1); if(j >= 0){ Dp[i] = j + P[p].h * P[p].h; insert(1, i, Dp[i] - P[p].h); } } if(Dp[N] > 0) printf("%I64d\n", Dp[N]); else puts("No solution"); memset(Dp, -1, sizeof(Dp)); } return 0; }

转载请注明原文地址: https://www.6miu.com/read-78994.html

最新回复(0)