One hundred layer HDU, hundredhdu

Source: Internet
Author: User

One hundred layer HDU, hundredhdu

One hundred layer HDU-4374

$ Sum [I] [j] [k] $ represents the sum of the column j to k in the I Layer

$ Ans [I] [j] $ indicates the maximum value that layer I eventually stays in column j, so obviously $ ans [I] [j] = max (ans [I-1] [j-t] + sum [I] [j-t] [j],..., ans [I-1] [j + t] + sum [I] [j + t] [j]) $

Obviously, simply following the equation, the time complexity $ O (nmt) $ cannot pass. But when we see max, we can think of using some RMQ methods to optimize it.

What is important here is a decomposition (not thought ):

$ Ans1 [I] [j] $ indicates the maximum value that goes down from layer I to the right and stays in column j.

$ Ans2 [I] [j] $ indicates the maximum value that the layer I goes down to the left and stays in column j.

After finding ans1 and ans2 for each row, $ ans [I] [j] = max (ans1 [I] [j], ans2 [I] [j]) $

So :( for convenience, a [j] indicates column j of row I)

$ Ans1 [I] [j] = max (ans [I-1] [j] + a [j],..., ans [I-1] [j-t] + a [j] +... + a [j-t]) $

$ Ans2 [I] [j] = max (ans [I-1] [j] + a [j],..., ans [I-1] [j + t] + a [j] + .. + a [j + t]) $

We also need to do some processing: (it seems that other people's code is directly uploaded? Is the status not fixed ?)

For ans1:

T = 2,

$ \ Begin {equation} \ begin {split} ans1 [I] [1] & = max (ans [I-1] [1] + a [1]) \
& = Max (ans [I-1] [1]) + a [1] \ end {split} \ end {equation} $
$ \ Begin {equation} \ begin {split} ans1 [I] [2] & = max (ans [I-1] [1] + a [1] + a [2], ans [I-1] [2] + a [2]) \
& = Max (ans [I-1] [1], ans [I-1] [2]-a [1]) + a [1] + a [2] \ end {split} \ end {equation} $
...

For ans2:

T = 1

$ \ Begin {equation} \ begin {split} ans2 [I] [m] & = max (ans [I-1] [m] + a [m]) \ & = max (ans [I-1] [m]) + a [m] \ end {split} \ end {equation} $
$ \ Begin {equation} \ begin {split} ans2 [I] [M-1] & = max (ans [I-1] [m-1] + a [M-1], ans [I-1] [m] + a [m] + a [m]) \ & = max (ans [I-1] [m], ans [I-1] [s-1]-a [m]) + a [m] + a [M-1] \ end {split} \ end {equation} $
$ \ Begin {equation} \ begin {split} ans [I] [m-2] & = max (ans [I-1] [m-2] + a [m-2], ans [I-1] [m-1] + a [m-2] + a [M-1]) \ & = max (ans [I-1] [m-2]-a [m]-a [m-1, ans [I-1] [m]-a [m]) + a [m] + a [M-1] + a [m-2] \ end {split} \ end {equation} $
.....

In this way, the optimization method is obvious. You can use a monotonous queue to maintain the minimum value of the sliding window.

Number of errors: Many

Error time:> 1 hour

Cause of error: Row 63 array names are marked incorrectly. ans2 is converted into ans1, which is not modified when the monotonous queue is copied and pasted.

 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<set> 5 using namespace std; 6 typedef pair<int,int> P; 7 int l,r,n,m,x,y,a[105][10010],ans[105][10010],ans1[105][10010],ans2[105][10010],sum[10010]; 8 int anss; 9 P q[10010],t;10 int main()11 {12     int i,j;13     while(scanf("%d%d%d%d",&n,&m,&x,&y)==4)14     {15         anss=-0x3f3f3f3f;16         memset(ans,140,sizeof(ans));17         for(i=1;i<=n;i++)18             for(j=1;j<=m;j++)19                 scanf("%d",&a[i][j]);20         ans[1][x]=a[1][x];21         for(i=1;i<=y;i++)22         {23             if(x+i<=m)    ans[1][x+i]=ans[1][x+i-1]+a[1][x+i];24             if(x-i>=1)    ans[1][x-i]=ans[1][x-i+1]+a[1][x-i];25         }26         for(i=2;i<=n;i++)27         {28             for(j=1;j<=m;j++)29                 sum[j]=sum[j-1]+a[i][j];30             l=r=0;31             for(j=1;j<=y+1;j++)32             {33                 t=P(ans[i-1][j]-sum[j-1],j);34                 while(l<r&&q[r-1].first<=t.first)    --r;35                 q[r++]=t;36                 ans1[i][j]=q[l].first+sum[j];37             }38             for(j=y+2;j<=m;j++)39             {40                 if(l<r&&q[l].second<j-y)    l++;41                 t=P(ans[i-1][j]-sum[j-1],j);42                 while(l<r&&q[r-1].first<=t.first)    --r;43                 q[r++]=t;44                 ans1[i][j]=q[l].first+sum[j];45             }46             sum[m+1]=0;47             for(j=m;j>=1;j--)48                 sum[j]=sum[j+1]+a[i][j];49             l=r=0;50             for(j=m;j>=m-y;j--)51             {52                 t=P(ans[i-1][j]-sum[j+1],j);53                 while(l<r&&q[r-1].first<=t.first)    --r;54                 q[r++]=t;55                 ans2[i][j]=q[l].first+sum[j];56             }57             for(j=m-y-1;j>=1;j--)58             {59                 if(l<r&&q[l].second>j+y)    l++;60                 t=P(ans[i-1][j]-sum[j+1],j);61                 while(l<r&&q[r-1].first<=t.first)    --r;62                 q[r++]=t;63                 ans2[i][j]=q[l].first+sum[j];64             }65             for(j=1;j<=m;j++)66                 ans[i][j]=max(ans1[i][j],ans2[i][j]);67         }68         for(j=1;j<=m;j++)69             anss=max(anss,ans[n][j]);70         printf("%d\n",anss);71     }72     return 0;73 }

Sum [I] [j] indicates the sum of the j to k columns in the I layer.
Ans [I] [j] indicates the maximum value that layer I stays in column j.
Ans [I] [j] = max (ans [I-1] [j-t] + sum [I] [j-t] [j],..., ans [I-1] [j + t] + sum [I] [j + t] [j])

** Decomposition:
Ans1 [I] [j] indicates the maximum value that goes down from layer I to the right and stays in column j.
Ans2.. Left ..
Ans1 [I] [j] = max (ans [I-1] [j] + sum [I] [j] [j],..., ans [I-1] [j-t] + sum [I] [j-t] [j])
T = 2
Ans1 [I] [1] = max (ans [I-1] [1] + a [1])
= Max (ans [I-1] [1]) + a [1]
Ans1 [I] [2] = max (ans [I-1] [1] + a [1] + a [2], ans [I-1] [2] + a [2])
= Max (ans [I-1] [1], ans [I-1] [2]-a [1]) + a [1] + a [2]
// = Max (ans [I-1] [1] + a [1], ans [I-1] [2]) + a [2]
// = Max (ans1 [I] [1], ans [I-1] [2]) + a [2]

Ans1 [I] [3] = max (ans [I-1] [1] + a [1] + a [2] + a [3], ans [I-1] [2] + a [2] + a [3], ans [I-1] [3] + a [3])
= Max (ans [I-1] [1], ans [I-1] [2]-a [1], ans [I-1] [3]-a [1]-a [2]) + a [1] + a [2] + a [3]
// = Max (ans [I-1] [1] + a [1] + a [2], ans [I-1] [2] + a [2], ans [I-1] [3]) + a [3]
// = Max (ans1 [I] [2], ans [I-1] [3]) + a [3]

Ans1 [I] [4] = max (ans [I-1] [2] + a [2] + a [3] + a [4], ans [I-1] [3] + a [3] + a [4], ans [I-1] [4] + a [4])
= Max (ans [I-1] [2]-a [1], ans [I-1] [3]-a [1]-a [2], ans [I-1] [4]-a [1]-a [2]-a [3]) + a [1] + a [2] + a [3] + a [4]
// = Max (ans [I-1] [2] + a [2] + a [3], ans [I-1] [3] + a [3], ans [I-1] [4]) + a [4]

Ans2 [I] [j] = max (ans [I-1] [j] + a [j], ans [I-1] [j + 1] + a [j] + a [j + 1],..., ans [I-1] [j + t] + a [j] + .. + a [j + t])
T = 1
Ans2 [I] [m] = max (ans [I-1] [m] + a [m])
= Max (ans [I-1] [m]) + a [m]
Ans2 [I] [M-1] = max (ans [I-1] [m-1] + a M-1], ans [I-1] [m] + a [m] + a [s-1])
= Max (ans [I-1] [m], ans [I-1] [s-1]-a [m]) + a [m] + a [M-1]
Ans [I] [m-2] = max (ans [I-1] [m-2] + a [m-2], ans [I-1] [m-1] + a [m-2] + a [M-1])
= Max (ans [I-1] [m-2]-a [m]-a [m], ans [I-1] [m]-a [m]) + a [m] + a [M-1] + a [m-2]

6 3 2 2
7 8 1 7 8 1
4 5 6 4 5 6
1 2 3 1 2 3

T = 1
Ans [2] [1] = max (ans [1] [1] + sum [2] [1] [1], ans [1] [2] + sum [2] [2] [1])
Ans [2] [2] = max (ans [1] [1] + sum [2] [1] [2], ans [1] [2] + sum [2] [2] [2], ans [1] [3] + sum [2] [3] [2])
Ans [2] [3] = max (ans [1] [2] + sum [2] [2] [3], ans [1] [3] + sum [2] [3] [3], ans [1] [4] + sum [2] [4] [3])

T = 2
Ans [2] [1] = max (ans [1] [1] + sum [2] [1] [1], ans [1] [2] + sum [2] [1] [2], ans [1] [3] + sum [2] [1] [3])
Ans [2] [2] = max (ans [1] [1] + sum [2] [1] [2], ans [1] [2] + sum [2] [2] [2], ans [1] [3] + sum [2] [2] [3], ans [1] [4] + sum [2] [2] [4])
Ans [2] [3] = max (ans [1] [1] + sum [2] [1] [3], ans [1] [2] + sum [2] [2] [3], ans [1] [3] + sum [2] [3] [3], ans [1] [4] + sum [2] [3] [4], ans [1] [5] + sum [2] [3] [5])
Ans [2] [4] = max (ans [1] [2] + sum [2] [2] [4], ans [1] [3] + sum [2] [3] [4], ans [1] [4] + sum [2] [4] [4], ans [1] [5] + sum [2] [4] [5], ans [1] [6] + sum [2] [4] [6])

T = 2
Ans [2] [1] = max (ans [1] [1] + a [2] [1], ans [1] [2] + a [2] [1] + a [2] [2], ans [1] [3] + a [2] [1] + a [2] [2] + a [2] [3])
Ans [2] [2] = max (ans [1] [1] + a [2] [1] + a [2] [2], ans [1] [2] + a [2] [2], ans [1] [3] + a [2] [2] + a [2] [3], ans [1] [4] + a [2] [2] + a [2] [3] + a [2] [4])
Ans [2] [3] = max (ans [1] [1] + a [2] [1] + a [2] [2] + a [2] [3 ], ans [1] [2] + a [2] [2] + a [2] [3], ans [1] [3] + a [2] [3], ans [1] [4] + a [2] [3] + a [2] [4], ans [1] [5] + a [2] [3] + a [2] [4] + a [2] [5])
Ans [2] [4] = max (ans [1] [2] + a [2] [2] + a [2] [3] + a [2] [4 ], ans [1] [3] + a [2] [3] + a [2] [4], ans [1] [4] + a [2] [4], ans [1] [5] + a [2] [4] + a [2] [5], ans [1] [6] + a [2] [4] + a [2] [5] + a [2] [6])

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.