In C, how do you prove that if no character appears more than floor(n/2) times, we can always achieve n%2 length of the final string? (where 'n' is the length of the given string) Please help.
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
In C, how do you prove that if no character appears more than floor(n/2) times, we can always achieve n%2 length of the final string? (where 'n' is the length of the given string) Please help.
Name |
---|
Let
First, notice that we can do some operation exactly when $$$\mathrm{maxf}_1 < n$$$.
Case 1: $$$n\equiv0\pmod{2}$$$
Claim: If $$$\mathrm{maxf}_1 \le n/2$$$ and $$$n > 0$$$, we can do an operation such that after the operation, $$$\mathrm{maxf}_1 \le n/2$$$.
Proof:
This means that if we have $$$\mathrm{maxf}_1 \le n/2$$$ and $$$\mathrm{maxf}_1 < n$$$, we can do an operation and keep the first condition satisfied. Thus, we can do operations while $$$n/2 < n\ \Leftrightarrow\ 0 < n/2\ \Leftrightarrow\ n > 0$$$. Thus we can always reach the state with $$$n = 0$$$. $$$\square$$$
Case 2: $$$n\equiv1\pmod{2}$$$
Claim: If $$$\mathrm{maxf}_1 \le \lceil n/2\rceil$$$ and $$$n > 1$$$, we can do an operation such that after the operation, $$$\mathrm{maxf}_1 \le \lceil n/2\rceil$$$.
Proof:
This means that if we have $$$\mathrm{maxf}_1 \le \lceil n/2\rceil$$$ and $$$\mathrm{maxf}_1 < n$$$, we can do an operation and keep the first condition satisfied. Thus, we can do operations while $$$\lceil n/2\rceil < n\ \Leftrightarrow\ (n+1)/2 < n\ \Leftrightarrow\ n+1 < 2n\ \Leftrightarrow\ 1 < 2n-n\ \Leftrightarrow\ n > 1$$$. Thus we can always reach the state with $$$n = 1$$$. $$$\square$$$