Submission : (https://mirror.codeforces.com/contest/1692/submission/275124032)
Problem : (https://mirror.codeforces.com/contest/1692/problem/E)
Why this approach fails, can anyone give some counter test case?
№ | Пользователь | Рейтинг |
---|---|---|
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 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
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 |
Submission : (https://mirror.codeforces.com/contest/1692/submission/275124032)
Problem : (https://mirror.codeforces.com/contest/1692/problem/E)
Why this approach fails, can anyone give some counter test case?
Название |
---|
Consider array
0 1 1 0 1 1 0
, target sum is 1. Clearly it can't be done in less than 5 operations, because you must remove at least two 0s to be able to remove at least three 1s. But your approach would remove both outer zeros and setzero
to 1, then remove 3 more 1s, leading to a total cost of 4. It doesn't take into account that both outer 0s had to be removed.Thank you, i haven't noticed it.
The correct answer is 4 your algorithm returns 3.
The reason it fails in this case is that it performs the correct steps but only counts removing zeros from both ends as 1 operation. (the last else statement in your loop)
If you correct it your approach will still be incorrect as in the case:
After the fix your algorithm will return 8, instead of the correct 5 (it remove the first 5 numbers)
As far as I know there is no greedy solution to the problem.
This problem is equivalent to finding a subarray of maximum size with a sum equal to K (the numbers you leave). This can be solved with a sliding window.
Thank you.
Yeah, i later tried sliding window and it worked, i was just curious whether it can be done with two pointers alone but it doesn't seem to work. thank you once again.