Блог пользователя Don_quixxote

Автор Don_quixxote, история, 9 месяцев назад, По-английски
Problem 1

Given a binary string find no. of subsequence of length 3 such that no two adjacent characters are same(101, 010). N <= 1e5

Problem 2

Given an array we want to make it non-increasing by doing following operation one at a time :- - you can shift 1 from a[i] to a[i+1], after that a[i]->a[i]-1 and a[i+1]->a[i+1]+1 (if i+1<n) - you can shift 1 from a[i] to a[i-1], after that a[i]->a[i]-1 and a[i-1]->a[i-1]+1 (if i-1>=0) Calculate minimum moves required to make it non-decreasing. N <= 250. a[i] <= 1e9 Example: a[] = [5 3 2 3 3 3] -> [4 4 2 3 3 3] -> [4 3 3 3 3 3], output is 2

Problem 3

Given an array a[] find no. of array b, such that 1 <= b[i] <= a[i], and no two adjacent element of b are same(b[i] != b[i+1]). Print it modulo (1e9+7) since ans is large. N <= 1e5 a[i] <= 1e6 Example: a[] = [2 3] b[] = [1 2], [1 3], [2 1], [2 3], output is 4

  • Проголосовать: нравится
  • -15
  • Проголосовать: не нравится

»
9 месяцев назад, скрыть # |
 
Проголосовать: нравится -10 Проголосовать: не нравится

I think there is issue in question 2 you write to make it non-decreasing but in example make it non-increasing.

»
9 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится -16 Проголосовать: не нравится

1,3 are pretty easy 1 is standard dp question and third one can be solved in 2 dp of size 2*n i can't think of second question for now but i feel it has same implementation as

will update the comment if i got any soln for 2

»
9 месяцев назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

You can solve problem 3 using a Lazy Segment Tree. Let's define dp[i][j] as no of arrays you can form with first i elements, with j as the last element.

Now, dp[i][j] = (Σ(dp[i — 1][k])) — dp[i — 1][j], where 1 <= k <= a[i]. Here, you're basically calculating all such arrays that end with i — 1 elements, with the last element not being j.

Your segment tree should store dp[i][j]. At each transition, you take the range sum (let's call it VAL) for dp[i][k], 1<=k<=a[i], then negate each element in the range 1<=k<=a[i], then add VAL to each position in this range. you have your next transition.

NOT AN EASY PROBLEM AT ALL.

»
9 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

for 3rd can we do this D&C :

solving for i,j => find maxm elememt :

suppose it is at k

k == i => ans is (a[k]-1)*solve(i+1,j) k == j => ans is (a[k]-1)*solve(i,j-1)

else : (a[k]-2)*(ways to solve i,j neglecting element at k) + (a[k]-1)*(ways to solve i,j neglecting element at k , arg (max(a[i],a[j])) )

neglecting an element means forget it is there and element prev and next to it are assumed to be adjacent

my code got TLE'd anyone knows a good way to implement this?

or this brute force cant be optimised?