洛谷 P5424 [USACO19OPEN]Snakes

Topic Link

Title Description

Legend, thousands of years ago eliminated the St. Patrick's Irish moo all snakes. However, snakes are now making a comeback! St. Patrick's Day is March 17 each year, so Bessie Moo use the complete removal of all snakes Ireland to commemorate St. Patrick's.

Bessie equipped with a fishing net for capturing the  N  snake set in row ( . 1 N . 4 0 0). Bessie must capture all snakes each group in the order the groups appear in this line. Whenever a group arrested after Bessie finished the snake, she would put the snake in a cage, and then with an empty fishing net to start capturing the next set.

A size  s  fishing net Bessie can catch any means include  g  a group of snake bar, where  g s. However, every time with the size of Bessie  caught a group of fishing nets  snake, it means a waste of  s - g space. Bessie may be arbitrarily set the initial size of the fishing net, and she can change  K  times the size of the fishing net ( . 1 K < N).

Please tell her Bessie minimum after completing all set to capture the snake can reach a total waste of space.

Topic analysis

This question is an obvious DP. We first consider a special case - for a range, if only to set the size of a fishing net, you need to set the size of the fishing net size is the largest group within the interval, wasted space is the maximum number of group minus the product of the space within the group and use. Then when the situation can be set n times, we enumerate the middle of the range of a point, to make it as a change in the n-th point, this time to change the strategy will be uniquely determined as above, but this time change before it has been calculated before.

Thus, setting F i, j is the j-th front i altering the size of the fishing net minimum wasted space, according to the above reasoning,

f i, 0 = maxof (1, i) * i sum (1, i), f i, j = max {f l, j-1 + maxof (l + 1, i) * (the) -sum ( l + 1, i)} (j ≠ 0, j-1 <= l <i).

Code

 1 #include<algorithm>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 int n,k,a[401],f[401][401],maxof[401][401],sum[401];
 6 int main()
 7 {
 8     scanf("%d%d",&n,&k);
 9     for(int i=1;i<=n;++i)
10     {
11         scanf("%d",&a[i]);
12         sum[i]=sum[i-1]+a[i];
13         maxof[i][i]=a[i];
14     }
15     for(int i=1;i<=n;++i)
16         for(int j=i+1;j<=n;++j)
17             maxof[i][j]=max(maxof[i][j-1],a[j]);
18     memset(f,0x7f/3,sizeof f);
19     for(int i=1;i<=n;++i)
20         f[i][0]=maxof[1][i]*i-sum[i];
21     for(int i=1;i<=n;++i)
22         for(int j=1;j<=min(i,k);++j)
23             for(int l=j;l<i;++l)
24                 f[i][j]=min(f[i][j],f[l][j-1]+maxof[l+1][i]*(i-l)-sum[i]+sum[l]);
25     printf("%d",f[n][k]);
26     return 0;
27 }
Snakes

 

Guess you like

Origin www.cnblogs.com/Psephurus-Gladius-zdx/p/11181364.html