A. Watermelon
Only if w can be presented as 2m + 2k watermelon can be divided to two even parts.So w = 2(m+k), where m, k ≥ 1, that is
(w ≥ 4 и w - even) - necessary and sufficient condition for watermelon dividing.
B. Before an Exam
Problem restrictions allow one to use algo complicated as much as he wants. In my opinion the most simple way is written below.- Fill studying time for each day sсhedulei with minTimei - Peter wants to study as less as he can
- Compute rest: need = sumTime - Σsсhedulei. if need < 0 than Peter can't satisfy lower bound of parents restrictions in schedule making: even if schedule is as small as possible he will go over the quota.
- In each day Peter has free studying hours: Δ = min(need, maxTimei - sсhedulei) - each day i Peter can additionally study. (With each day sсhedulei increase on Δ and need decrease on Δ).
- if need > 0 than Peter can't satisfy lower bound: studying with max power he has no time.
C. Registration system
First, one can see the restrictions ≤ 105, that is he can expect n*log(n) solution.Next, each string consist of "all lowercase Latin letters", that is it doesn't contains digits.
Because of it for each string we can compute what time we see it in the input. One needs to write "OK" for first request of current string, otherwise <string><computed number>.
This numbers can be computed by associate array (map in C++) or simple by sorting array of:
(string, number in input file),
sorted array would be splitted to blocks with equal strings. In each block one can set numbers from 1 to block_size.
D. Mysterious Present
Problem can be solved by dynamic programming approach for O(n2) by time and O(n) by memory.- Sort all envelopes by pairs (wi, hi) or by product wi*hi in nondecreasing order. Because of it envelope i can be put in envelope j only if i < j. That is for any chain {a1, a2, ..., an} is true that a1 < a2 < ... < an.
- Just delete all envelopes in which card can't be put in.
- Add to sorted envelopes array a0 - Peter's card.
- Introduce DP function: dp[i] maximum chain length which ends in i-th envelope.
- dp[0] = 0, dp[i] = max(dp[j]) + 1, where i ≥ 1, 0 ≤ j < i and j can be put in i. This function can be easily got by previously computed values of it.
For each dp[i] one can remember index p[i] = j with which maximum reached in formula p5. If one knows s - envelope index in which maximal chain ends he can restore all answer sequence (i0 = s, ik+1 = p[ik]).
2. Simultaneously we will get that all chains will start in Peter's letter.
PS: In my solution from the contest I was creating chains from big envelopes to small (in decreasing order) and I add fake envelope with (w0, h0) = (+inf, +inf), dp[0] = 0. Because of I didn't catch that letter itself is better candidate to chain origin. And next I was choosing between chain ends in which card can be put in.
Second way - you can write to organizers and ask specific test on specific task. But do not abuse with it. This is the last that you need to do.