**A: Carrot Cakes**

**B: T-shirt buying**

**C: Fountains**

**D: Field expansion**

**E: Aquarium decoration**

**F: Beautiful fountains rows**

**G: Cut the pie**

# | User | Rating |
---|---|---|

1 | tourist | 3539 |

2 | V--o_o--V | 3338 |

3 | Um_nik | 3307 |

4 | Petr | 3297 |

5 | ecnerwala | 3282 |

6 | LHiC | 3266 |

7 | wxhtxdy | 3264 |

8 | Radewoosh | 3257 |

9 | Vn_nV | 3182 |

10 | dotorya | 3178 |

# | User | Contrib. |
---|---|---|

1 | Radewoosh | 207 |

2 | Errichto | 177 |

3 | neal | 159 |

4 | Ashishgup | 158 |

5 | PikMike | 157 |

6 | Petr | 156 |

6 | majk | 156 |

8 | rng_58 | 155 |

9 | Um_nik | 154 |

9 | 300iq | 154 |

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

↑

↓

Codeforces (c) Copyright 2010-2018 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jan/22/2019 23:07:11 (f2).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

What a beatiful solution for D )

What a beautiful solution for E

Someone plz explain me the solution for Carrot Cakes ?

You can simulate the entire process on time ( Since constraints are low. The maximum time it would take to get 1000 cakes is 1e6)

First situation : You only have first oven. Then calculate the time you need to get atleast n cakes.

Second situation : From starting you have first oven. At time t = d , you have second oven too. Simultaneously calculate the time needed to get atleast n cakes.

If time required in second situation is less than first situation , then it is reasonable to build the second oven.

Code

Your simulation seems a bit too complicated. Just forget about the second oven and check whether or not any cakes remain at time d: http://codeforces.com/contest/799/submission/27016758

Won't your code go wrong as per this statement:

If the time needed with the second oven is the same as with one oven, then it is unreasonable.I'm not able to format a test-case so just giving the description of a test-case: If cakes remain after 'd' time but time completion of both procedures is same.

Please correct me if I'm wrong.

Sorry, I got it! Ignore my above comment..!

You can also solve it by calculating how many cakes you can bake before the second oven ends...

I tried solving problem D this way !!!But I am having some problem :( I am getting WA !!!! Can someone please help me regarding the same :)

Can you please explain the problem D bit more?

Edit:I got itCould someone explain this algorithm?

You can find quite short code here http://codeforces.com/blog/entry/48868

For Carrot Cakes, can someone explain how do we get the value (n+k-1)/k*t ?

You need ceil(n/k)*t because you bake k cakes every t seconds and that is the smallest time you need to bake atleast n cakes.

ceil(a/b) is the same as floor((a+b-1)/b) or equivalently, (a+b-1)/b.

We don't need to use bin search in C if we precalcaulated two better fountains for each p <= c and p <= d (I named it C_dp[c+1][2], D_dp[d+1][2]). Then, first ans will be (C_dp[c][0] + D_dp[d][0]) if both of them are non-zero.

Another two cases — we will check each sorted fountain buying for C, and if its cost < p (we still have some money), trying to by second fountain C_dp[c — p][0] (if b of current fountain == C_dp[c — p][0], then we use C_dp[c — p][1]). Also checking if it is non-zero.

The same with D's.

Summary: O(C + D + C * log(C) + D * log(D)) (log for sorting). http://codeforces.com/contest/799/submission/27062024

I don't know why minuses, it's a good approach. However you do not need to sort.

Basically, split fountains into 2 groups {diamonds, coins} and calculate 2 best beauties for each price.

You can do this in a similar way to prefix sum. Store each beauty for initial price (remember max 2 of them). Then go from smallest prices to largest: you have 2 best beauties for price p-1, add them to price p, and leave best 2 for price p.

Then process every fountain f — you have to find the best match for f. You should choose either the best beauty for diamonds-price_in_diamonds(f) or coins-price_in_coins(f). You should store 2 of them, as the best one might be the same as f in which case you have to choose the second best.

Time: O(maxPrice + n) — including coins and diamonds initially possessed.

Ok, I see. We needn't sort because a[i] <= 10^5. Your solution is better, thanks.

Overtaking editorial for E:

If we were able to get to this point, the answer clearly exists — now improve it.

Notice that if we have k items which both players like, then we choose nothing in 2. and we got all the best in 3. So we may remove some of the items taken in 1., to improve the result.

If we got less than k items in step 1, notice that we got all the best in 2. and 3. We obviously don't remove anything from 3. We also don't remove anything from 2 — we took all the best items, to meet the requirement that both of players have to get at least k items.

Which means that we can improve our answer by removing things we got in 1.

We do this in 2 ways:

A. Check the balance of each player. If it has more than k items, you can try to remove item liked by both. There are 2 cases, when you can remove: A.1. Both players have more than k items, then you can replace the current worst item collected in 1. with the best item not collected yet and decrease the number of items for both players.

A.2. If only one player (call him p1) has more than k items, the other has exactly k items (call him p2) but there are still some items which p2 likes (obviously we will not choose anything liked by both, as we selected all the best such items in 1. and now we remove them). We can remove item from 1. and get the best item which p2 likes. Decrease the status for p1 and nothing changed for p2 — it sill has k items.

B. Once we removed everything by A., both players have exactly k items and we can't decrease the status anymore. We still can improve the answer though. We need to remove 2 items taken so far. One of them must be from 1., so the other must not be liked by any player. We replace them with 2 best items: 1 of them liked by p1 and the other by p2. In this way we do not change the number of items which both players like (they still have k of them) and we keep the set of m items.

After A. and B. we finish with the best set possible.

Can someone find the bug in my code for C problem. I am getting TLE. code Thanks in advance.

"After that we need to calculate linear dynamic where di equals to maximum width which we got for the length i. Let's iterate through extensions in sorted order and recalculate d." I did'nt find this to be clear enough. Can someone please explain this ?

Let dp[i][x] = the biggest height you can get with width x using i extensions

to calculate next 2 states (multiplying width & height)

dp[i+ 1][x] =dp[i][x] *ext[i+ 1] // multiply heightdp[i+ 1][x*ext[i+ 1]] =dp[i][x] // multiply widththe base case is

dp[0][w] =hwe can reduce the memory and use only 1D array and update states in decreasing order of width

after each new state

dp[i][x] =y, check if (x * y) can enclose the (a*b) rectangle27060296

dp[i+1][x*ext[i+1]] = max(dp[i][x*ext[i+1]], dp[i][x])Could you explain, please, why dp[i+1][x*ext[i+1]] isn't just always equal to dp[i][x*ext[i+1]]? Can't figure whence have you got dp[i][x].

The max height for width

x*ext[i+ 1] usingiextensions can be less than max height for widthxusingiextensions, so we take the max of previous max height for that width (dp[i][x*e]) and max height for the width we are multiplying (dp[i][x])Also the previous max height can be zero

For example:

w= 2,h= 2, extensions {2, 2}This worst case

O(n^{2}) solution for C has passed. It should not have if the test cases had been a little stronger.Should be noted that for E you don't need any particular data structure (such as a stl set) to keep your data. You can just use arrays and remember the last index of pushed "must-have" elements so you can't push them on further steps (in order to mentain O(n)) steps. This way you get O(n) as opposed to O(n log n). Not much of an improvement but it's something.

could someone explain me how to use Binary Index Tree in C problem?? Any help would be appreciable

I don't understand problem C's solution, why should we consider p1 equals p2? In fact, I accepted without considering this case (My code)).

could someone please explain the logic of problem c.

Can you please explain the solution for D?

Dynamic Programming

Meet in the Middle

I have seen solutions of C using fenwick tree( also called Binary Indexed Tree or BIT) such as this one : (http://codeforces.com/contest/799/submission/27019364) Can somebody explain this ?

Solution D

Can anybody explain approach to problem D?

can anyone give a code with explanation for [problem:D].

Problem D:

I found this is the easiest soln to understand. Note this gets TLE...So first one gets AC just for optimization