# How do I solve 629D on CodeForces

## Far Relative’s Birthday Cake

## 题目链接：

http://www.codeforces.com/problemset/problem/629/A

## 解题思路：

Consider that we have *row**i* chocolates in the *i*'*th* row and *col**i* chocolates in the *i*'*th* column.

The answer to the problem would be: . It is obvious that every pair would be calculated exactly once (as we have no more than one chocolate in the same square)

Time Complexity: *O*(*n*2)

## AC代码：

## Far Relative’s Problem

## 题目链接：

http://www.codeforces.com/problemset/problem/629/B

## 解题思路：

Consider that we have *boy**i* males in the *i*'*th* day of the year and *girl**i* females in the *i*'*th* day of the year. These arrays can be filled easily when you are reading the input (See the code). Then for the *i*'*th* day of the year, we could have 2 * *min*(*boy**i* , *girl**i*) people which could come to the party. The answer would be maximum of this value between all days *i* (1 ≤ *i* ≤ 366)

Time Complexity: *O*(366**n*)

## AC代码：

## Famil Door and Brackets

## 题目链接：

http://www.codeforces.com/problemset/problem/629/C

## 解题思路：

This problem can be solved with dynamic programming:

1. Calculate *dp**i*, *j* : How many sequences of brackets of length *i* has balance *j* and intermediate balance never goes below zero (They form a prefix of a valid sequence of brackets).

2. For the given sequence of length *n* calculate the resulting balance *a* and the minimum balance *b*.

3. Try the length of the sequence added at the beginning *c* and its balance *d*. If - *b* ≤ *d* then add *dp**c*, *d* * *d**m* - *n* - *c*, *d* + *a* to the answer.

Time complexity: *O*((*n* - *m*)^2)

## AC代码：

## Babaei and Birthday Cake

## 题目链接：

http://www.codeforces.com/problemset/problem/629/D

## 解题思路：

First of all, we calculate the volume of each cake: *v**i* = π × *h**i* × *r**i*2.

Now consider the sequence *v*1, *v*2, *v*3, ..., *v**n* : The answer to the problem is the maximum sum of elements between all increasing sub-sequences of this sequence. How do we solve this? First to get rid of the decimals we can define a new sequence *a*1, *a*2, *a*3, ..., *a**n* such that

We consider *dp**i* as the maximum sum between all the sequences which end with *a**i* and

*dp**i* =

The answer to the problem is: π × *max**i* = 1*n**dp*[*i*]

Now how do we calculate ? We use a max-segment tree which does these two operations: 1. Change the *i*'*t* member to *v*. 2. Find the maximum value in the interval 1 to *i*.

Now we use this segment tree for the array *dp* and find the answer.

Consider that *a*1, *a*2, *a*3, ..., *a**n* is sorted. We define *b**i* as the position of *a**i*. Now to fill *dp**i* we find the maximum in the interval [1, *b**i*) in segment and we call it *x* and we set the *b**i* th index of the segment as *a**i* + *x*. The answer to the problem would the maximum in the segment in the interval [1,n]

Time complexity: *O*(*nlgn*)

## AC代码：

- Which are the most massive chillout album
- How is OrderedDict implemented in Python
- How lucky is the number 6
- How do I say Batman in Spanish
- Which U S banks are accepting Bitcoin
- When did Dobby die
- How is Code Monk of HackerEarth com
- How do software crackers crack programs
- How are Jimmy Johns franchise locations evaluated
- How do I start bidding
- Where get experienced painters
- Who started collecting data
- Which headphones should I buy
- How does AngelList make money
- What is co op gaming
- Will America discriminate against Lebanese Christians
- Where are mortgage rates today
- Does caffeine affect blood glucose levels