Fangbo's corn field (tree array optimization) (3-D partial order)

Keywords: PHP

zzn Tumor Question...

First of all, the question has a nature:

When choosing the height of the increase interval, we should increase the left endpoint of the selected increase to n all, because we require

The final monotonic non-decreasing sequence has the longest length, so it can increase steadily without losing money.

Then we find that the optimal height of the last point of this question is determined by a[n]+K

Set the array meaning f[j][k] to represent the longest sequence length of current maize height j, K operations

And we can think of this as a three-digit partial order:

1. Current maize position i, transferred from 1-i-1

2. Current maize height j, transferred from 1-j

3. Current corn has been operated on k, transferred from 1-k-1

Maintain j,k with a tree array. (Obviously....)

(To save memory, our first loop is i, which ensures that our I is ordered and does not need to be written in an array.

Actually, i,j,k, either of them is OK, just to save memory....)

Note:

1. Cycle K in reverse order. (Obviously.... If the current K is in reverse order, its insertion will affect the enumeration of the current bit k)

2. Pre-processing Note that depending on your enumeration, you can also not

3. An value does not necessarily select the last point

 

 1 #include<map>
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<string>
 8 #include<vector>
 9 #define int long long
10 using namespace std;
11 int c[511][5501];
12 int a[11001];
13 int maxn;
14 int lowbit(int x){return x&(-x);}
15 int n,K;
16 void add(int x,int y,int sum)//x Which operation y height k No. k operation
17 {
18     for(int i=x;i<=K+1;i+=lowbit(i))
19     {
20         for(int j=y;j<=maxn+K;j+=lowbit(j))
21         {
22               c[i][j]=max(c[i][j],sum);
23         }
24     }
25 }
26 int query(int x,int y)
27 {
28      int ans=0;
29      for(int i=x;i>=1;i-=lowbit(i))
30      {
31           for(int j=y;j>=1;j-=lowbit(j))
32           {
33               ans=max(ans,c[i][j]);
34           }
35      }
36      return ans;
37 }
38 int ans=0;
39 signed main()
40 {
41     scanf("%lld%lld",&n,&K);
42     for(int i=1;i<=n;++i)
43     {
44          scanf("%lld",&a[i]);
45          maxn=max(a[i],maxn);    
46     }
47     for(int i=1;i<=n;++i)
48     {
49         for(int j=K;j>=0;--j)
50         {
51              int th=query(j+1,a[i]+j)+1;
52              //printf("dp[%lld][%lld]=%lld\n",j,a[i]+j);
53              ans=max(th,ans);
54              add(j+1,a[i]+j,th);   
55         }
56     }
57     //int ans=query(K+1,a[n]+K);
58     printf("%lld\n",ans);
59 }

Posted by johany on Tue, 30 Jul 2019 09:52:16 -0700