http://mirror.codeforces.com/problemset/problem/446/A
Can anyone help with the approach to this problem. Read the tutorial but wasn't able to get the idea.
# | 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 | 160 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
8 | awoo | 154 |
10 | luogu_official | 151 |
http://mirror.codeforces.com/problemset/problem/446/A
Can anyone help with the approach to this problem. Read the tutorial but wasn't able to get the idea.
Name |
---|
I will try to explain the logic with an example : Consider the given case :
6
7 2 3 1 5 6
Step1 : Make the subegments from the given sequence such that each segment is strictly increasing.i.e the required subsegments are : {7},{2,3},{1,5,6}
Step2 : iterate over all the subsegments by taking two consecutive subsegments at a time i.e from the above input we will get two cases : {7},{2,3}
{2,3},{1,5,6}
Now taking {7},{2,3} : we can observe that the maximum required subsegment length we can form is 3 i.e by changing 7 from first segment to 1 we can get {1,2,3}. Now taking {2,3},{1,5,6} : here in this case the maximum required subsegment length we can form is 5 i.e by changing 1 from second segment to 4.so the new subsegment formed is {2,3,4,5,6} of length 5. So the final answer is max(3,5)=5.
Now let us take another example :
4
1 1 1 1
From step1 ,we can form subsegments like : {1},{1},{1},{1}
Step2 : iterate over all the subsegments by taking two consecutive subsegments at a time i.e from the above input we will get two cases : {1},{1} -> considering 1,2
{1),{1} -> considering 2,3
{1},{1} -> considering 3,4
Now taking {1},{1} : we can observe that the maximum required subsegment length we can form is 2 i.e by changing 1 from first segment to 0 we can get {0,1}.OR 1 from second segment to 2, we can get {1,2}.Any how you can get max length as 2. Now the another 2 cases are similar to first So final answer is max(2,2,2)=2
Note: Try to think how this approach works and also try to implement the logic.In case you cannot check my code : http://mirror.codeforces.com/contest/447/submission/9161790 I think you will understand that. If you have any doubts/require some more examples comment below.
Lets say given array is A[].
first of all let say I[] and D[] are two arrays.
I[i] = tells you the length of the longest increasing sub segment you can have which ends at i(index).
D[i] = tells you the length of the longest decreasing sub segment which starts at i.
For example :
given array : 7 2 3 1 5 6
I[i] : 1 1 2 1 2 3
D[i] : 1 2 1 3 2 1
you can construct I[] and D[] in O(n). Trivial cases:
for any i we can change A[i] to a suitable integer to construct a segment with I[i-1] + 1 length.
again for any i we can change A[i] to a suitable integer to construct a segment with D[i+1] + 1 length.
remember we can change A[i] to any integer positive or negative.
So our answer is the overall maximum length.
for the 3rd case : when changing a element i we have to check whether we can change A[i], as A[i] should be A[i-1]<A[i]<A[i+1].
My code : code