https://codeforces.me/problemset/problem/1180/A
Isn't the time complexity of the problem I mentioned above is o(1) ? In the tutorial they are saying o(n) ?
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | djm03178 | 152 |
https://codeforces.me/problemset/problem/1180/A
Isn't the time complexity of the problem I mentioned above is o(1) ? In the tutorial they are saying o(n) ?
Название |
---|
Well, you can also implement the solution in $$$O(1)$$$ like in this submission. The trick here that the rhombus is made out of $$$1+3+5+7+...+7+5+3+1$$$ so, you can sum all $$$N-1$$$ elements they will be $$$1+3+5+7+...+2(N-1)-1$$$. Which is basically the sum of all odd numbers starting from 1 till the $$$(N-1)th$$$ term. The summation is known to be $$$N^2$$$. Lets proof it with induction:
$$$f(1)=1$$$
$$$f(x)=f(x-1)+2x-1$$$
$$$f(x)=f(x-1)+2x-1=x^2$$$
$$$f(x-1)=f(x-2)+2(x-1)-1=(x-1)^2$$$
$$$f(x-1)=f(x-2)+2x-3=(x-1)^2$$$
$$$f(x)=f(x-2)+2x-3+2x-1=x^2$$$
$$$f(x)=f(x-2)+4x-4=N^2$$$
Subtracting $$$f(x)$$$ with $$$f(x-1)$$$:
$$$f(x-2)+4x-4-f(x-2)-2x+3=x^2-(x-1)^2$$$
$$$2x-1=x^2-(x-1)^2$$$
$$$2x-1=x^2-x^2+2x-1$$$
$$$2x-1=2x-1$$$
Therefore, it is correct for all integers $$$>=1$$$.
And because the last $$$N-1$$$ equal to first $$$N-1$$$ terms, we will multiply it by 2. What is left is to add the row which have the most cells. In 1, it was 1. In 2, it was 3. So, it keeps increasing by 2. So we will add $$$2N-1$$$ as it keeps increasing by 2 and -1 is the calibration constant. Therefore, $$$2((N-1)^2)+(N*2-1)$$$ is the final answer.
Other simpler way is we can notice that for each next-n, we can split the adding area it to 4 regions of the same area
So by the fact above, the number of areas are $$$1 + 1 \times 4 + 2 \times 4 + 3 \times 4 + \cdots + n \times 4 = 1 + 4 * (1 + 2 + \cdot + n) = 1 + 4 \times \frac{n (n - 1)}{2}$$$