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

Автор guptaji30, история, 5 лет назад, По-английски

there are 3 types of queries

0 X add a number X, 1 X remove the number X (X always exist), 2 X return the number of subsets that sum to X,

0<=X<=10^3 0<=number of queries <=10^3

I tried to implement this using the knapsack approach but its bound to give TLE

any suggestions? Any help would be appreciated

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

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +21 Проголосовать: не нравится

I am a bit confused regarding the time complexity here.

X <= 1000, and number of elements is also at-max 1000.

In the worst case, assume 1 insertion and then 1 subset query alternating one after another.

500 subset queries and for each query you'll need O(N*|X|) operations. So the total operation turns out to be around 500*500*1000 ≈ 2.5e8 operations? Maybe brute forcing using knapsack won't give TLE.

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +130 Проголосовать: не нравится

I think its very common to add in knapsack in O(size), similarly we can delete from knapsack also in O(size).

Code

Note that the addition part is quite easy, and the subtraction also works the same way(it is clear that the order doesn't matter when we add in the knapsack, intutively you can assume that the last element added was the one to be deleted and try to think how would you undo it).

Hope it helps.

»
5 лет назад, скрыть # |
Rev. 5  
Проголосовать: нравится -27 Проголосовать: не нравится

We could solve this in $$$ Q * X * sqrt(Q)$$$ with square root decomposition, i already explained this on our group to someone. Hope at that time it was not live.

So basically you can decompose array into square root buckets or now when you remove the element, you can recalculate the current knapsack bucket, as you know the all the element in square root bucket. this will handle for the update part. Complexity of update is $$$ X * sqrt(Q)$$$ , as there will be only sqrt elements

Now for the query part you know the knapsack states, so you can iterate on square root buckets to get number of ways to get sum = X, complexity will be same $$$ X * sqrt(Q)$$$, for finding the ways part to get sum = X, on first two buckets its just $$$ \sum _{i = 0} ^{ i = X} ways1[i] * ways2[X-i] $$$, similary you can keep combining those buckets and after this remaining atmost square root element, can be added in this in same way as we did in update.



Another way is kind of FFT and segtree, i am not sure about this approach, but we can build convolution tree, someway, i will think this in more detail and write the answer.

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

    imagine people downvoting just because it had some downvotes before and not reading the approach,lol
    this is also one way to solve this question also feasible btw.
    The most funny thing is above i made same comment one is getting upvoted and one is downvoted, what kind of people are on codeforces, anyways, no wonder i have stopped caring about downvotes.

»
5 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится +6 Проголосовать: не нравится

I think knapsack is the way to go. Because the third type of queries only ask for subset sums of $$$X \lt =10^3$$$, we only need the knapsack DP array to be of length 1001. Say we have calculated the DP array for some numbers, then it's easy to calculate a new DP array in $$$O(maxX)$$$, extending the set of numbers by adding number X (query type add). Remove queries make it harder. We can use offline deletion from datastructure only supporting insertion and a stack of DP arrays to answer all the queries offline, and support delete. This only costs an extra log factor, and would give a runtime of $$$O(maxX*Q*log(Q))$$$

Edit: Oh, removing is just as easy as adding, because instead of adding, we know subtract all DP transitions. My approach could still be useful for queries of the form: Is a subset sum of X possible?

  • »
    »
    5 лет назад, скрыть # ^ |
    Rev. 3  
    Проголосовать: нравится -14 Проголосовать: не нравится

    you could use number of ways to check if possible or not, like creating a array with subset sum = p * mod,p is whole number. have less probability of failing for random mod and when you randomise more by using more number of mods, chances of failing is even less, for reference you could see code of my friend above for the codeforces edu problem.

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +2 Проголосовать: не нравится

What is goc33?

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

You didn't write the correct constraints. The actual constraints were 7*1e3 for both 'X' and the queries. That is surely going to make a difference right?

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

Would it be possible for you to share the first question of your test as well?? It would really help!!

»
5 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +5 Проголосовать: не нравится

Its addition and deletion on knapsack, I saw this idea here IZho017 bootfall