### spookywooky's blog

By spookywooky, history, 17 months ago,

Hello,

I am trying to solve https://cses.fi/problemset/task/1163, but got some problem to understand it. It states that "There is a street of length x whose positions are numbered 0,1,…,n. " That should be "0, 1,..., x", not n, isn't it?

Then the example:

Input:
8 3
3 6 2

Output:
5 3 3


I think output should be 5, 3, 2, not 5, 3, 3. Since obviously the longest segment without a traffic light is 2, not 3.

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


Can somebody explain? A lot of people solved that problem, I think I am missing something. Thanks.

• +4

 » 17 months ago, # |   +3 Found it by myself ;)Two things one must notice:There is an "implicit" traffic light after position 0. The length of the segment is calculated as without left bound, but including right bound.So, the traffic lights are placed kind of "right of the positions". After adding the three lights, it looks like 0 1 2 3 4 5 6 7 8 x x x x The longest segment is the one "4,5,6", of length 3.
•  » » 6 months ago, # ^ | ← Rev. 2 →   0 spookywooky Can You Explain it in simple words.I am also stuck at this problem but not able to get it.Problem Statement is not clear.Thanks in Advance!PS-Why this post is getting downvoted?I have done nothing wrong.If asking a doubt is an offense to community then why a discuss section was made on Codeforces.This discuss section is made for discussing doubts and problems.
•  » » » 6 months ago, # ^ |   0 Which part is unclear? The statement or the solution?
•  » » » » 6 months ago, # ^ |   0 I am not getting the statement! i think the output should be 5 3 2 and not 5 3 3
•  » » » » » 6 months ago, # ^ |   0 The traffic lights are placed between the numbered segments of the street, after the given number.In the testcase above there are 3 lights, at positions between 2 and 3, between 3 and 4, and between 6 and 7.So the longest segment without a traffic light is {4,5,6} of size 3.
•  » » » » » » 6 months ago, # ^ | ← Rev. 3 →   0 After adding 6 wouldn't be answer 4 Because we have 3 segments which do not contain traffic lights : 0 1  1 2  2 3 and size is 4. I am sorry if its a dumb question but i can request if you can clarify this! spookywooky ?
•  » » » » » » » 6 months ago, # ^ |   0 Nah, the numbers are not the spaces between the segments, the numbers are the segments.There is a segment 1, a segemnt 2 etc... We place the lights between the segments.
•  » » » » » » » » 6 months ago, # ^ |   0 You mean to say 0 1 is segment 1  1 2 is segment 2  2 3 is segment 3 and so on... spookywooky?
•  » » » » » » » » » 4 months ago, # ^ |   0 light only belongs to the left segmentlight on 1 means 1-1 and 2-n
 » 17 months ago, # |   +1 You are right, the task statement had a typo. This has now been fixed, thanks!
•  » » 6 months ago, # ^ |   0 it is still 5 3 3 i guess, https://cses.fi/problemset/task/1163
•  » » 6 months ago, # ^ |   0 Fixed? Nothing has been changed. Can you recheck?
 » 14 months ago, # |   0 Can you please give me a hint on how to solve this problem? I am stuck in it.
•  » » 14 months ago, # ^ |   0 Hint: Think about the number of segments that could be affected after adding a new traffic light.
•  » » » 14 months ago, # ^ |   +3 Thanks for the hint man
•  » » » » 14 months ago, # ^ |   0 Hey, I am still not able to figure it out, can you share your approach?
•  » » » » » 14 months ago, # ^ |   +1 Solution Codeint x, n, a[200002]; set s; multiset m; cin >> x >> n; s.insert(0); s.insert(x); for(int i = 1; i <= n; ++i) { cin >> a[i]; auto l = s.lower_bound(a[i]), r = --s.upper_bound(a[i]); if(*l > a[i]) --l; if(*r < a[i]) ++r; auto f = m.find(*r - *l); if(f != m.end()) m.erase(f); m.insert(a[i] - *l); m.insert(*r - a[i]); s.insert(a[i]); cout << *--m.end() << ' '; } 
•  » » » » » » 14 months ago, # ^ | ← Rev. 2 →   0 Thank you :) Hey, If you know how to approach "Throwing dice"? https://cses.fi/problemset/task/1096
•  » » » » » » » 14 months ago, # ^ |   +1 it is an easy linear reccurence:$f(x) = \sum_{i=1}^{6} f(x-i)$ which can be solved by the matrix multiplication
•  » » » » » » » 14 months ago, # ^ |   +1 Solutionyou can solve it using dynamic programming$f(i) = \sum_{k = 1} ^ {6} f(i - k)$$-> \ O(n)$$after \ that \ you \ can \ use$ matrix exponentiation$-> \ O(log(n))$ I know you can build the table!$0, 0, 0, 0, 0, 1$ $1, 0, 0, 0, 0, 1$ $0, 1, 0, 0, 0, 1$ $0, 0, 1, 0, 0, 1$ $0, 0, 0, 1, 0, 1$ $0, 0, 0, 0, 1, 1$then the answer after is at row 1 column 6Good luck! Code#include #define int long long using namespace std; const int N = 2e5 + 5, mod = 1e9 + 7; struct matrix { vector > a; matrix(int n, int m) { a.assign(n, vector (m)); } matrix operator * (const matrix &b) const { int n = a.size(), m = b.a.size(), p = b.a[0].size(); matrix c(n, p); for(int i = 0; i < n; ++i) { for(int j = 0; j < p; ++j) { for(int k = 0; k < m; ++k) { c.a[i][j] = (c.a[i][j] + (a[i][k] * b.a[k][j]) % mod) % mod; } } } return c; } }; matrix binpow(matrix n, int k) { int s = n.a.size(); matrix ans(s, s); for(int i = 0; i < s; ++i) { ans.a[i][i] = 1; } if (k < 0) return ans; while(k) { if(k & 1) ans = ans * n; n = n * n; k >>= 1; } return ans; } signed main() { int n; cin >> n; if(!n) return cout << n, 0; matrix k(6, 6), ans(6, 6); k.a = {{0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 1}, {0, 1, 0, 0, 0, 1}, {0, 0, 1, 0, 0, 1}, {0, 0, 0, 1, 0, 1}, {0, 0, 0, 0, 1, 1}}; k = binpow(k, n - 6); ans.a[0] = {1, 2, 4, 8, 16, 32}; if (n <= 6) return cout << ans.a[0][n - 1], 0; ans = ans * k; cout << ans.a[0][5]; } 
•  » » » » » » » » 14 months ago, # ^ |   0 Thank you very much! I did not know about the matrix multiplication part, it look's really cool.
•  » » » » » » » » » 14 months ago, # ^ |   +1 it is written in his book, page 221