Sgu 183. Painting the bils

Source: Internet
Author: User

Time Limit: 0.25 s

Space limit: 4 m

Question:

In N (n <= 10000) balls, color several balls at the cost of Ci, making any continuous M (M <= 100) at least two balls are colored.

 

 

 

 

 

 

 

Solution:

First, we can think of a DP state transition equation directly.

F [I] [J] indicates that the first I ball is applied and the last colored ball is the minimum cost of J.

F [I] [J] = min (F [J] [k]) + CI, k> I + 1-m

The time complexity of analyzing this transition equation is O (n * m). The data range of this question is as high as 10 ^ 8.

Obviously, we need a better solution.

By analyzing the above equation, we found that when calculating min (F [J] [k]), there is a part of repeated calculation,

So I tried to reduce this repetitive process.

For a J, the I range is (J + 1, J + s-1)

The range of corresponding K is (I + 1-m + 1 )~ J-1)

If we push I from (J + m-1) to (j + 1)

You can make K from (J-1) to (I + 1-m + 1)

Min (F [J] [k]) the range to be calculated increases sequentially and can be obtained recursively.

That is, min (F [J] [k]) can be obtained in O (1) time.

The total time complexity becomes O (N * m)

We can find that N * m arrays cannot be used directly in the space, and we can optimize the scrolling array.

Code

# Include <iostream> # include <cstring> using namespace STD; const int mod = 101; int n, m; int C [10009]; // F [I] [J] applies the current I-th ball, and the I-th, J-th ball at the minimum cost. // only the last 200 balls are retained; int f [200] [200]; int main () {CIN> N> m; For (INT I = 1; I <= N; I ++) cin> C [I]; memset (F, 0x3f, sizeof F); F [1] [0] = C [1]; for (INT I = 1; I <= m; I ++) for (Int J = 1; j <I; j ++) f [I] [J] = C [I] + C [J]; for (Int J = 2; j <n; j ++) {int TEM = 0x3f3f3f; for (INT I = J + m-1; I> J; I --) {if (I <= m) break; TEM = min (TEM, f [J % mod] [(I-m) % mod]); F [I % mod] [J % mod] = TEM + C [I];} int ans = 0x7fffffff; For (INT I = N-m + 1; I <= N; I ++) for (Int J = I-1; i-j <M & N-j <m; j --) ans = min (ANS, F [I % mod] [J % mod]); cout <ans; return 0 ;}

  

Sgu 183. Painting the bils

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.