Array of integers is unimodal, if: * it is strictly increasing in the beginning; * after…
- User
Array of integers is unimodal, if:
- it is strictly increasing in the beginning;
- after that it is constant;
- after that it is strictly decreasing.
The first block (increasing) and the last block (decreasing) may be absent. It is allowed that both of this blocks are absent.
For example, the following three arrays are unimodal: [5, 7, 11, 11, 2, 1], [4, 4, 2], [7], but the following three are not unimodal: [5, 5, 6, 6, 1], [1, 2, 1, 2], [4, 5, 5, 6].
Write a program that checks if an array is unimodal. Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of elements in the array.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1 000) — the elements of the array.
Output
Print "YES" if the given array is unimodal. Otherwise, print "NO".
You can output each letter in any case (upper or lower).
Examples
Input
6 1 5 5 5 4 2
Output
YES
Input
5 10 20 30 20 10
Output
YES
Input
4 1 2 1 2
Output
NO
Input
7 3 3 3 3 3 3 3
Output
YES
Note
In the first example the array is unimodal, because it is strictly increasing in the beginning (from position 1 to position 2, inclusively), that it is constant (from position 2 to position 4, inclusively) and then it is strictly decreasing (from position 4 to position 6, inclusively).
Assistant
We need to check if the array is unimodal according to definition: Strictly increasing in the beginning; then constant; then strictly decreasing. The increasing and decreasing parts may be absent. So we can have only constant, or only increasing then constant, or constant then decreasing, or increasing then constant then decreasing, or just increasing (if constant part absent? Actually definition says after increasing it is constant; after that it is strictly decreasing. But also says increasing block and decreasing block may be absent. Does it say constant block may be absent? It says: "it is strictly increasing in the beginning; after that it is constant; after that it is strictly decreasing. The first block (increasing) and the last block (decreasing) may be absent." It does not explicitly say constant block may be absent. But in examples, [4, 4, 2] is considered unimodal. That's constant then decreasing? Actually [4,4,2] has constant (first two equal) then decreasing. That fits. Also [7] is constant only? Actually [7] has single element: it's trivially constant? It's unimodal. Also [10,20,30,20,10] is increasing then decreasing? Wait definition says after increasing it is constant, then decreasing. But that example: 10,20,30,20,10. That is strictly increasing from 10 to 30, then strictly decreasing from 30 to 10. There is no constant part. Yet example says YES. So constant part may be absent? The definition says "after that it is constant; after that it is strictly decreasing." But they also said "The first block (increasing) and the last block (decreasing) may be absent. It is allowed that both of this blocks are absent." They didn't say constant block may be absent. However, from examples, [10,20,30,20,10] lacks constant block, but is considered unimodal. So perhaps the constant block can also be absent? Or maybe they consider that a constant block of length 1 (the peak) is allowed? Actually the peak is 30, but it's a single element; it's not constant with other elements because there's only one. But the definition "constant" implies at least one element, but a single element can be considered constant (since it doesn't change). So that fits: there is a constant block of length 1 at the peak. So the sequence is increasing, then constant (the peak), then decreasing. That's exactly what they have. Similarly, [5,7,11,11,2,1] has constant block of two 11's. [4,4,2] has constant block of two 4's? Actually it's constant then decreasing: there is increasing? It starts with two 4's. That means increasing block absent, constant block present, then decreasing. That's okay. [7] is just constant block only (increasing and decreasing absent). [1,2,1,2] not unimodal because after decreasing it increases again.
Thus basically we need to check that the array first non-decreasing? Actually increasing strictly, then constant, then strictly decreasing. But note that strictness: increasing part must be strictly increasing (no equal). Constant part can be any number of equal elements. Decreasing part must be strictly decreasing (no equal). Also there should be no extra patterns like constant after decreasing etc.
Thus the array should have at most one "peak" region: it can go up (strictly), then possibly flat, then down (strictly). And cannot go up again after down, etc.
We can think of scanning through and checking that the sequence of differences changes pattern at most twice: from increasing to constant to decreasing. But careful: allowed patterns: (1) only constant; (2) only increasing (then constant maybe zero length? Actually if only increasing, then no constant? But definition says after increasing it is constant, then decreasing. For only increasing array, e.g., [1,2,3] is that unimodal? According to definition: it is strictly increasing in the beginning; after that it is constant; after that it is strictly decreasing. If only increasing, then after increasing, we would have constant part but it is absent? But they didn't say constant can be absent. However, consider if the array is [1,2,3]. Is this unimodal? The examples didn't include such, but we can reason. The definition says the first block (increasing) and last block (decreasing) may be absent. It does not say constant may be absent. So maybe constant must exist. But then [1,2,3] would not be unimodal because after increasing there is no constant, just end of array. But is that considered constant of length zero? Possibly not. However, many solutions on Codeforces treat it as unimodal if it's non-decreasing then non-increasing? Actually typical "unimodal array" problem (CF 831A?) maybe similar. Wait this is problem 831A? I think it's Codeforces 831A - Unimodal Array. Yes that's exactly the problem. In Codeforces 831A, the conditions: The array is unimodal if it can be split into three parts: strictly increasing, constant, strictly decreasing. The first and last parts may be missing. It does not say the middle part can be missing, but in discussion, many participants considered that if the array is strictly increasing then constant is of length 1 at the end? Actually if array is strictly increasing and ends, then constant part would be the last element? But constant part requires at least one element, but the last element can be considered constant? However, if we have increasing and then the array ends, we haven't had a constant block before a decrease. The definition says after increasing it is constant, then after that it is decreasing. If there is no decreasing, then after constant it would be decreasing absent, but constant must be present? Actually in the phrase: "it is strictly increasing in the beginning; after that it is constant; after that it is strictly decreasing." This structure expects three blocks in order. If decreasing part is absent, that means after constant the array ends; that's allowed because decreasing block may be absent. So constant must be present? But if increasing block is absent, we have constant then decreasing; that's okay. If both increasing and decreasing absent, we have constant only. So constant appears always? Actually if both increasing and decreasing absent, we have constant only, so constant is present. In case of only increasing, we would need to have constant after increasing, but then no decreasing. That would be increasing then constant. Is that allowed? The definition says decreasing may be absent. So yes, that would be allowed if constant is present. But can constant be of zero length? The blocks are defined as contiguous sequences. Usually block length can be zero? They didn't say. In typical interpretation of such problems, the constant part can be of length 1 (i.e., just the peak) but not zero. So an array that is strictly increasing only (like [1,2,3]) would not have a constant part; after the last element, there is no constant block, because it's not constant (it's still increasing until the end). So to be unimodal, you must have at least one element that is not part of a strictly increasing segment immediately before the end. In other words, the array must have a "plateau" or a peak that is not followed by a strict increase. But many accepted solutions for 831A treat strictly increasing as unimodal? Let's check the official problem statement: "Array of integers is unimodal, if: it is strictly increasing in the beginning; after that it is constant; after that it is strictly decreasing. The first block (increasing) and the last block (decreasing) may be absent. It is allowed that both of this blocks are absent." That's exactly what I have. They do not say the constant block may be absent. I think in typical solutions, they consider that a strictly increasing array is not unimodal because it lacks the constant part. But let's verify with examples: They gave [7] as YES. That's constant only. They gave [4,4,2] as YES (constant then decreasing). They gave [5,7,11,11,2,1] as YES (increase, constant, decrease). They gave [5,5,6,6,1] as NO. That's increase? Actually [5,5,6,6,1] goes: 5,5 (constant) then 6 (increase) then 6 (constant?) Actually pattern: start constant? Let's not worry. They gave [1,2,1,2] NO. They gave [3,3,3,3,3,3,3] YES (constant only). They did NOT give an example of strictly increasing like [1,2,3]. I recall that in CF 831A, some participants were confused but the intended solution is that the array must have at most one change from increasing to constant to decreasing. Many accepted solutions treat strictly increasing as unimodal? Actually let me think: If array is [1,2,3], then it is strictly increasing. According to definition: we need it to be strictly increasing in the beginning, then constant, then strictly decreasing. Since constant is not present, it doesn't fit the three-block description. But could we treat constant as of length zero? The statement says "The first block (increasing) and the last block (decreasing) may be absent." It does not say constant block may be absent. So constant must be present. Therefore [1,2,3] would be NO. However, many might argue that the constant block could be considered as the last element (since it's a single element, it's constant with itself). But that's not how block is defined: a block is a contiguous subarray. The constant block requires that for any two adjacent elements within it, they are equal. In a strictly increasing array of length > 1, the last element does not form a constant block of length > 1; but if length 1, it's constant trivially. But if length > 1, the last element alone would be a constant block of length 1? But then the increasing block would be the elements before it. But then the increasing block ends before the constant block. So consider [1,2,3]: we could split as increasing [1,2] and constant [3]? But check: after increasing, we need constant; is [3] constant? Yes, a single element is constant. And then decreasing is absent. So that would satisfy: increasing, constant, (no decreasing). But is the increasing part strictly increasing? Yes, 1,2. So this would be valid if we allow constant block of length 1 that is not equal to previous element? Wait, the constant block must follow the increasing block. That means the last element of increasing block is adjacent to first element of constant block. For [1,2,3], if increasing block is [1,2], then constant block starts at index 3? Actually array indices: [1,2,3]. If increasing block is [1,2], then the next element is 3. The constant block would be [3]. But constant block's first element is 3. For constant block, adjacent elements must be equal. Since it's only one element, that's okay. However, we need to check the transition between increasing block and constant block: The condition is that after the increasing block, the array becomes constant. That means that the element after the increasing block must be equal to the last element of increasing block? Actually no: if it's strictly increasing, then each element is greater than previous. So after the last element of increasing block (2), the next element (3) is greater than 2. That would not be constant; it's still increasing. So to go from increasing to constant, we require that the first element of constant block is equal to the last element of increasing block (since constant means no change). Actually if we have increasing block ending at i, and constant block starting at i+1, then we need a[i] < a[i+1]? No, that would be increasing. To be constant, we need a[i] == a[i+1]. So for [1,2,3], after 2 we have 3 which is >2, so it's not constant, it's still increasing. So you cannot split into increasing then constant because the transition is not equality. So [1,2,3] would not satisfy. The only way to satisfy the definition is that after the strictly increasing part, we have a segment where the values stop increasing and become equal. So there must be at least one pair of equal adjacent elements after the strictly increasing part (if the increasing part is non-empty). If the increasing part is empty, then constant part can start directly; if decreasing part is empty, then after constant the array ends.
Thus [1,2,3] is not unimodal because there is no point where it becomes constant; it just increases to the end. Similarly, a strictly decreasing array would not be unimodal because there is no constant before decreasing? Actually if it's strictly decreasing from the start, then we need constant first? Since increasing absent, we could have constant then decreasing. So a strictly decreasing array would need constant block before decreasing, but if it's strictly decreasing from the first element, then there is no constant at the start; the first two elements are decreasing, not constant. So [3,2,1] is not unimodal? Could we have constant block of length 1 at the start? Then decreasing after? If constant block is [3] then decreasing block is [2,1]; but the transition from constant to decreasing requires that after constant block we have a smaller element? Actually decreasing block is strictly decreasing, so the first element of decreasing block must be less than the constant block's value? Strictly decreasing means each element is less than the previous. So if constant block is [3], then next element must be less than 3, which is fine. However, the requirement is that the array is constant at the beginning if increasing absent. So we could have constant block [3] (single element) then decreasing [2,1] with 2<3 and 1<2. That would satisfy? But check the transition: constant block has all equal values. Since it's only one element, it's fine. Then the next element must be strictly less than the previous? For strictly decreasing, we require a[i] > a[i+1] for each adjacent pair in decreasing block. For the transition between constant and decreasing, if constant block ends at index i, then a[i] (constant) and a[i+1] (first of decreasing) should satisfy a[i] > a[i+1] to start decreasing. In [3,2,1], if we take constant = [3], then a[1]=3, a[2]=2, and 3>2, so that's okay. Also the decreasing block itself is strictly decreasing: 2>1, okay. So [3,2,1] would be considered unimodal? But is that allowed? The problem statement says "first block (increasing) and last block (decreasing) may be absent." So if increasing absent, we have constant then decreasing. In [3,2,1], is there a constant block? The first block (constant) must be such that it is constant (all equal). If the array starts with [3,2,...], then the first two elements are not equal, so we cannot have a constant block of length >1. But we could have constant block of length 1? Typically, a block is a contiguous subsequence; length can be 1. So [3] is constant (trivially). Then the remaining [2,1] is strictly decreasing. So that fits: constant (first block) present, increasing absent, decreasing present. Thus [3,2,1] would be unimodal according to this reasoning. But is that intended? In many solutions to CF 831A, [3,2,1] is considered unimodal? I'm not sure. Let's test with known accepted outputs: In the problem statement, they gave [4,4,2] as YES: that's constant then decreasing. They didn't give a strictly decreasing example. However, many participants might have assumed that strictly decreasing is not allowed because there is no constant part before it? But as argued, constant part can be of length 1, i.e., just the first element. So strictly decreasing array of length >1 would have constant block of length 1 at the start, then decreasing. That would satisfy. But does the problem consider that as valid? Let's check the statement: "it is strictly increasing in the beginning; after that it is constant; after that it is strictly decreasing." If increasing absent, then it starts with constant. That constant block may be of any length, including 1. So yes, [3,2,1] should be YES. Similarly, strictly increasing array would need constant after increasing; that would require at least one equality after the increase. So strictly increasing without plateau is not allowed. That asymmetry is interesting but plausible.
Let's verify with CF editorial? I recall that the solution is to check that the array increases, then stays constant, then decreases, and the transitions happen at most once each. Many accepted solutions count the number of times the comparison changes (from < to = to >) and ensure it's not like < then > without = in between. Actually typical solution: iterate and check that we never see a < after seeing a > (i.e., no increase after a decrease), and also we never see a = after seeing a >? Wait. The condition is that the array should be non-decreasing then non-increasing? But strictness conditions: increasing must be strict, constant allowed, decreasing strict. However, we can model with comparisons.
Simplify: We want to ensure that the array is unimodal. We can simulate the expected phases: start in phase 0 (increasing). Actually we can have three phases: 1: increasing (strict), 2: constant, 3: decreasing (strict). We start in phase 1 if there is any increasing, but we might start in phase 2 if first elements are constant, or even phase 3? But phase 3 should come after constant. However, if increasing absent, we start in phase 2 (constant). But can we start directly in phase 3? That would mean decreasing from start, but with no constant before it. According to definition, if increasing absent, we have constant then decreasing. So decreasing cannot appear without constant before it. So the array must start with either increasing or constant, never decreasing. Actually if increasing absent, we have constant at beginning. So the first element(s) must be either part of increasing (strictly increasing from start) or constant. But if the array starts with decreasing (i.e., a[1] > a[2]), then the first block would be decreasing, which is not allowed because decreasing block is only after constant. So [3,2,1] does start with constant? The first element alone can be considered constant, but then the second element is less, so that's constant then decreasing. So the first block is constant (length 1) not decreasing. So it's fine. So the first two elements cannot be strictly decreasing if we want to consider constant block of length 1? Actually they can be strictly decreasing: if we have [3,2,...], we can interpret as constant block of length 1 then decreasing. That's allowed. So it's okay.
Now what about [1,2,3]? It starts with increasing (since 1<2). The second pair is 2<3, still increasing. At the end, we don't have constant, so we are stuck in increasing phase when array ends. That's not allowed because we need to transition to constant before end (since decreasing absent but constant must be present). Unless we consider constant of length 0? But they said constant block can be absent? Not explicitly. So [1,2,3] would be NO.
Check [1,2,2,3]? That would be increase (1<2), then constant? 2=2 constant, then increase 2<3 - that's increase after constant, which violates because after constant you can only go to decreasing or end. So not unimodal.
Thus we need to check that the sequence goes through at most two transitions: from < to =, and from = to >. It may start with = (if increasing absent) and then go to >, or start with < then go to = then optionally to >, or just =. It must never have < after > (i.e., increase after a decrease) or = after > (i.e., constant after decreasing), or < after =? Actually after = we can go to > (decrease) or end, but cannot go to < (increase). And after > we cannot go to anything except continue >; cannot go to = or <. Also we cannot have = then = then < etc. So basically we want to ensure that we never see an increase after we have seen a decrease. Also we want that once we see a decrease, we never see an equal? Actually equal after decrease would be constant after decreasing, which is not allowed (since decreasing must be strict). So after we see a >, we cannot have >=? Actually must have strictly decreasing, so if we have a[i] == a[i+1] after we are in decreasing phase, it's invalid. Also if we have a[i] < a[i+1] after decreasing, that's increase after decrease, invalid.
Also we need to ensure strictness: in increasing phase, must be strictly increasing, so cannot have a[i] == a[i+1] during increasing; if we encounter equality, that's transition to constant phase. In constant phase, must be equal; if we encounter less than, that's transition to decreasing; if we encounter greater than, that's invalid (increase after constant). In decreasing phase, must be strictly decreasing; if we encounter less than or equal, it's invalid.
We also need to consider that the constant phase may be of length 1 (a single element) or more. And it's okay to have no decreasing phase (i.e., end after constant or after increasing? But after increasing we must have constant, so if no decreasing, we end after constant. So if we are in increasing phase and we reach end without encountering equality, that's invalid because we didn't transition to constant. But could we have increasing then end directly? That would be like [1,2,3] which we argued is not unimodal. Let's confirm with official stance: In Codeforces 831A, many solutions accept [1,2,3] as unimodal? I'm not certain. Let's search memory: I recall a problem "Unimodal Array" on CF where the condition was: The array is unimodal if it is strictly increasing, then constant, then strictly decreasing, and the first and last parts may be missing. I think the intended interpretation is that the constant part may also be missing? Actually reading the statement again: "The first block (increasing) and the last block (decreasing) may be absent. It is allowed that both of this blocks are absent." It doesn't say the middle block may be absent. So constant must be present. In many editorial solutions, they check that the array first increases (strictly), then stays constant, then decreases (strictly), and they allow that the increasing or decreasing parts may be of length zero. But they also allow constant part to be of length zero? I need to recall the official solution. I think typical solution is: find the maximum element(s). The array should be non-decreasing up to the first occurrence of max, and non-increasing after the last occurrence of max. Also all elements equal to max should form a contiguous segment. Then it's unimodal. That approach would consider [1,2,3] as unimodal because max=3 occurs once, array up to max is non-decreasing (actually strictly increasing) and after max there are no elements, so it's fine. And the max segment is contiguous (just one). So that would say YES. So that approach would accept strictly increasing. Let's test with [1,2,3] using that logic: up to the maximum (3) the array is [1,2,3] which is non-decreasing (strictly increasing is okay). After the maximum, no elements. So it's unimodal. That's a common solution. Also [3,2,1] would be okay: maximum is 3 at start, before max there are no elements, after max it's non-increasing (strictly decreasing). So it's okay. Actually that approach would also accept arrays where there is a plateau of equal maximum. But it also requires that the maximum appears in a contiguous block, and that the array is non-decreasing up to the first max, and non-increasing after the last max. This includes cases where there is no plateau: if max occurs once, it's fine. It also allows constant block of length 1. So indeed [1,2,3] is accepted.
Let's see if any accepted solution on Codeforces 831A rejects [1,2,3]. I'm not sure. I'd like to check the official editorial: I recall the problem 831A, the editorial says: "We need to check if there exists an index i such that a[1] < a[2] < ... < a[i] >= a[i+1] >= ... >= a[n] and if i is the point where the maximum is located, also note that there can be a plateau of equal maximum elements." Actually they often use condition: The array is unimodal if it is non-decreasing up to some point and non-increasing after that point. But the problem statement requires strictness in increasing and decreasing parts. The editorial might say: we can check by removing duplicates? Wait careful: The statement says strictly increasing and strictly decreasing, but they allow constant parts. So if we check non-decreasing up to the max and non-increasing after, that would allow equalities before the max, which would violate strict increasing? Actually if there is an equality before the max, that would be constant part before the peak, but according to definition, constant part should only be after the increasing part and before decreasing. If we have equality before the max, that would be constant before increasing? For example, [2,2,3,1]? Let's see: Is [2,2,3,1] unimodal? It starts with constant (2,2) then increases to 3 then decreases to 1. According to definition, increasing block absent? Actually it starts with constant (so increasing absent), then constant, then increasing? Wait after constant we need decreasing? Actually the structure: if increasing absent, then it is constant then decreasing. But in [2,2,3,1], after constant (2,2) we have increase (2 to 3). That is not allowed because after constant we can only go to decreasing (or end). So [2,2,3,1] should be NO. If we check non-decreasing up to max: max is 3, array up to first max is [2,2,3] which is non-decreasing (2<=2<=3). That's fine. After last max, array is [3,1] which is non-increasing (3>=1). That would say YES, but it's actually NO per definition because there is increase after constant. So the non-decreasing condition is too permissive. It must be strictly increasing before the constant part, not allowing equalities before the peak unless they are part of a constant segment that starts after the increasing? Actually if there is an equality before the peak, that would imply there is a constant block before any increase, which violates the order: the definition says the array can start with increasing OR constant. So it could start with constant, but then after constant, it must be decreasing (if increasing absent) OR if increasing present, constant must be after increasing. But if it starts with constant and then increases, that would be constant then increasing, which is not allowed because after constant we cannot have increasing (only decreasing or end). So indeed array cannot have an increase after a constant block unless that constant block is preceded by an increase? Wait the order is: increasing (optional), then constant (mandatory?), then decreasing (optional). So the constant block must appear after any increasing, and before any decreasing. So if there is a constant block at the start (no increasing before it), then what comes after it? Only decreasing, not increasing. So if we have constant then increasing, that's invalid.
Thus the structure is: there is at most one change from increasing to constant, and at most one change from constant to decreasing. Also the array can start in constant (i.e., first block is constant) and then go to decreasing. It can also start in increasing and then go to constant and then maybe to decreasing. But it cannot have constant then increasing. So we need to enforce that once we leave increasing (if we were in it), we cannot go back to increasing; once we go to constant, we cannot go to increasing; once we go to decreasing, we cannot go to anything else. Also the array must not have two constant segments separated by something else.
Thus we can implement by scanning and tracking the current phase: 0=increasing, 1=constant, 2=decreasing. But we must allow the possibility that the initial phase may be constant if the first two elements are equal? Actually if the array length is 1, it's trivially unimodal (constant). For length >=2, we need to look at comparisons.
Better approach: Let's define the expected order of comparisons: We should see a pattern of comparisons between adjacent elements. The allowed transitions:
- Start: can be '<' (increasing) or '=' (constant) or maybe no comparisons (if n=1). But note that if start is '<', then we are in increasing phase; we can continue with '<' or move to '=' (constant) but never to '>'.
- If start is '=', then we are in constant phase; we can continue with '=' or move to '>' (decreasing), but not to '<'.
- If we ever see '>', we are in decreasing phase; we can only continue with '>'. Any '=' or '<' after that is invalid.
- Also note: we cannot see '<' after '=' (increase after constant) because that would be constant then increasing, which is not allowed.
- We also cannot see '<' after '>' obviously.
- We also cannot see '=' after '>' (since decreasing must be strict).
- Also we cannot have '<' then directly '>' without passing through '='? Because that would be increasing then decreasing with no constant in between. According to definition, after increasing it must be constant before decreasing. So '<' then '>' is invalid. Example: [1,3,2] would be 1<3 then 3>2, that's direct increase then decrease, which is not allowed because there is no constant block. But wait example [10,20,30,20,10] is allowed: 10<20, 20<30, 30>20, 20>10. That's increase then decrease without constant? But 30>20 is a decrease after increase. However, note that there is a single element 30 which can be considered as constant block of length 1. But the transition from increasing to constant: when we go from 20 to 30, that's increase, so still increasing. Then from 30 to 20, that's decrease. There is no equality. So does that violate the rule? In that array, there is no equality, so there is no constant block of length >1. But as argued, the peak element 30 could be considered as constant block of length 1. However, in terms of comparisons, between 20 and 30 we have '<', and between 30 and 20 we have '>'. So the sequence of comparisons is '<', '<', '>', '>'. This contains a direct transition from '<' to '>' (between the second and third comparisons? Actually the comparisons are: compare a1,a2 => '<', compare a2,a3 => '<', compare a3,a4 => '>', compare a4,a5 => '>'. So the sequence of comparison results is: <, <, >, >. There is no '='. But is it allowed to go from '<' directly to '>'? According to the definition, after increasing it must be constant, then decreasing. So if we consider the peak element as constant block of length 1, then the transition from increasing to constant is at the peak element. But in comparisons, there is no equality, because constant block of length 1 does not produce any comparison of equal elements. The transition from increasing to constant is not captured by a comparison, because constant block is just a single element that doesn't have a pair. So the comparison-based approach must allow the pattern where after one or more '<', we may see a '>' (as long as it's not preceded by a '='). However, if there is a constant block of length >1, we would see '=' comparisons. So we need to allow the case where there is a single element peak, i.e., we can have increasing then decreasing without an intermediate equality in comparisons. So the condition about needing a constant block is not that there must be an equality comparison; it's that there must be a point where the sequence stops increasing and starts decreasing, and the element at that point is the peak. That peak is considered the constant block (of length 1). So the condition on comparisons is that we should not have an increase after a decrease, and we should not have an equality after a decrease, and we should not have an equality before an increase? Actually need to formalize.
Better: The array is unimodal if there exists an index i (1 <= i <= n) such that:
- For all j < i: a[j] < a[j+1] (strictly increasing up to i)
- For all j > i: a[j] > a[j+1]? Actually if i is the peak, then after i, the array should be strictly decreasing: for all j >= i: a[j] > a[j+1]? But careful: if i is the peak, then for indices after i, we need a[i] > a[i+1], a[i+1] > a[i+2], ..., a[n-1] > a[n] if n>i. That means the decreasing part is strictly decreasing.
But also there could be a plateau: multiple equal maximum values. In that case, we have a segment [L, R] where values equal to max. Then condition: For all j < L: a[j] < a[j+1] (strictly increasing up to L), and for all j >= R: a[j] > a[j+1] (strictly decreasing after R). Also within [L,R] all values equal. This accommodates both cases: if plateau length 1, then L=R.
Thus the array is unimodal if there exists a contiguous segment of maximum values (i.e., the maximum occurs in a contiguous block) and the array is strictly increasing up to that segment and strictly decreasing after it. Additionally, the array before the segment must be strictly increasing (so no equalities before the segment), and after the segment strictly decreasing (no equalities after). However, what about array that is constant then decreasing? That means there is no increasing part; then the maximum segment is at the beginning. Condition: For all j < L (but L=1, so no j), vacuously true. For after R, strictly decreasing. Also segment is contiguous and contains maximum. That works. Similarly, increasing then constant: maximum segment at end, after segment no elements, so decreasing condition vacuous, and before segment strictly increasing. Also constant only: segment is entire array, before and after vacuous.
Thus the condition can be implemented by:
- Find the maximum value M.
- Find the first index where a[i] == M (call L).
- Find the last index where a[i] == M (call R).
- Check that for all i from 1 to L-1: a[i] < a[i+1] (strictly increasing).
- Check that for all i from R to n-1: a[i] > a[i+1] (strictly decreasing).
- Also check that all elements from L to R are equal to M (obviously by definition they are, but also need to ensure there is no dip inside: but if we found L and R as first and last occurrence of M, then by definition all between are M only if the maximum appears contiguously? Actually we must verify that M appears only in that contiguous block. Because if M appears later again after a lower value, that would violate the decreasing condition. But if we take L as first occurrence, R as last occurrence, then we need to ensure that there is no index i with a[i] != M between L and R. Since L is first occurrence, any index between L and R might have a value less than M, but then that would be less than M, contradicting that M is maximum? Actually if there is an index k between L and R with a[k] < M, then M is still maximum, but that value is not M, so the maximum values are not contiguous. That would be invalid. So we must check that for all i in [L,R], a[i] == M. That is equivalent to checking that all occurrences of M are contiguous. So we can check that the array does not have a pattern like ... M, x, M where x<M.
Thus algorithm:
- Read n and array.
- If n == 1: print "YES".
- Else:
- Find M = max(a).
- Find L = first index of M (0-based or 1-based).
- Find R = last index of M.
- Check that for all i from 0 to L-2? Actually for indices before L: for i=0 to L-2? Let's be careful: We need strictly increasing from start to L: i.e., for i=0 to L-1, a[i] < a[i+1] (since L is first occurrence of max; before L, each step must be strictly increasing). That also ensures no equalities before L. Also we need that a[0] up to a[L] are strictly increasing; but note that a[L] is M, and a[L-1] < a[L] must hold if L>0. If L==0, no condition.
- Check that for all i from R to n-2: a[i] > a[i+1] (strictly decreasing from R to end). That ensures after R, each step strictly decreasing.
- Check that for all i from L to R, a[i] == M (i.e., no non-M in between). This is automatically true if we verify that all values between L and R equal M, but we must ensure that there is no drop inside.
- Additionally, we might also need to check that before L, there are no equalities (already covered by strict increase). And after R, no equalities (covered by strict decrease).
- If all checks pass, print YES else NO.
Let's test with examples:
Example 1: 6: 1 5 5 5 4 2 M=5, L=1 (index 1, 0-based?), actually 0-based: a[0]=1, a[1]=5, a[2]=5, a[3]=5, a[4]=4, a[5]=2. L=1, R=3. Check before L: i=0 to L-1? L>0, check a[0] < a[1]? 1<5 true. Also need check for i from 0 to L-2? Actually strictly increasing condition from start to L means for each i from 0 to L-1, a[i] < a[i+1]. With L=1, we only need to check i=0: a[0] < a[1] true. If L>1, we'd need multiple checks. After R: from R to n-2: i=3 to 4? n=6 => indices 0..5. Check a[3] > a[4]? 5>4 true; a[4] > a[5]? 4>2 true. Also check a[R] > a[R+1]? Actually we need strictly decreasing from R to end, so we check for i=R to n-2: a[i] > a[i+1]. That includes i=3,4,... up to n-2. Also need that from R to n-1, values are strictly decreasing; that includes the step from R to R+1 and after. So check i=3 and i=4. Good. Also check that all from L to R are M: indices 1,2,3 are 5,5,5 OK. So YES.
Example 2: 5: 10 20 30 20 10 M=30, L=2, R=2 (only one occurrence). Before L: check i=0: a[0] < a[1]? 10<20 true; i=1: a[1] < a[2]? 20<30 true. So strictly increasing from start to L. After R: check i=2: a[2] > a[3]? 30>20 true; i=3: a[3] > a[4]? 20>10 true. Also all between L and R are M (just index 2). So YES.
Example 3: 4: 1 2 1 2 M=2? Actually max is 2. Occurrences: index 1 (value 2) and index 3 (value 2). L=1, R=3. Check before L: i=0: a[0] < a[1]? 1<2 true. So far ok. Check after R: from R to n-2: i=3? n=4 => indices 0..3, so n-2=2. Wait R=3 > n-2, so there are no i from R to n-2 because R > n-2? Actually we need to check decreasing from R to end, but R is last max. If R == n-1, then there are no elements after R, so decreasing condition vacuously true. But here R=3 (0-based) and n=4 => last index 3, so after R there is no element, decreasing condition vacuous. But also we need to check that after R, there are no elements, but also we need to check that from R to the end it's strictly decreasing? Actually from R to end: if R is the last index, there's no step after, so it's okay. But also we need to check that the segment between L and R is all M. That's not true because index 2 (between L and R) has value 1, which is not M. So check fails => NO. Good.
Example 4: 7: all 3's. M=3, L=0, R=6. Before L: no condition. After R: no condition. Check that all between L and R are M: yes (all 3). So YES. Also note we don't need to check increasing/decreasing because those parts are empty.
Edge Cases:
-
Strictly increasing: e.g., [1,2,3]. M=3, L=2, R=2. Before L: check i=0: 1<2 true; i=1: 2<3 true. After R: R is last index, so no after condition. All between L and R (just index2) is M. So YES. So this approach says YES. But earlier we thought definition might require constant block. However, the definition allows constant block of length 1 (the peak). So that's consistent.
-
Strictly decreasing: [3,2,1]. M=3, L=0, R=0. Before L: none. After R: check i=0: 3>2 true; i=1: 2>1 true. All between L and R (index0) is M. So YES.
-
Increase then constant then increase? e.g., [1,2,2,3]. M=3, L=3, R=3. Before L: check i=0:1<2 true; i=1:2<2? That's false because need strict < but 2==2. So fails. So NO. Good.
-
Constant then increase then decrease? e.g., [2,2,3,1]. M=3, L=2, R=2. Before L: check i=0:2<2? false; i=1:2<3 true, but i=0 fails because we need strict increase from start to L. So NO. Good.
-
Constant then decrease then increase? e.g., [5,5,4,5]. M=5, occurrences: index0=5, index1=5, index3=5. So L=0, R=3? Actually last occurrence of 5 is index3. But there is index2=4 between? Check contiguous condition: between L and R we must all be M, but index2=4 not M, so fails => NO.
-
Increase, constant, decrease but with extra equal after decrease? e.g., [1,2,2,1,1]. M=2, L=1,R=2? Actually a[1]=2, a[2]=2, then a[3]=1, a[4]=1. After R: check i=2: a[2]>a[3]? 2>1 true; i=3: a[3]>a[4]? 1>1 false (equal). So fails.
-
Increase, constant, decrease but with equal at end? [1,2,2,1,0] is fine because 1>0 strict.
-
Edge case: n=2. For [1,2]: M=2, L=1,R=1. Before L: check i=0:1<2 true. After R: none. So YES. That is strictly increasing? Actually it's increase then end, but considered unimodal because peak at end. For [2,1]: M=2, L=0,R=0. Before L: none. After R: i=0:2>1 true. So YES. That's strictly decreasing. For [2,2]: M=2, L=0,R=1. Check that all between L and R are M: yes. Before L: none. After R: none. So YES (constant). For [1,1,2]? Actually n=3, [1,1,2]: M=2, L=2,R=2. Before L: need strictly increasing from start to L: i=0:1<1? false (equal), so NO. So not unimodal because we had constant then increase.
Thus this algorithm aligns with typical solutions for CF 831A.
Now verify with some tricky cases:
-
[1,2,3,3,2,1] is YES: M=3, L=2,R=3. Before L: check i=0:1<2, i=1:2<3 ok. After R: i=3:3>2, i=4:2>1 ok. All between L,R are 3. Good.
-
[1,2,3,2,3,2,1]? M=3, occurrences: indices 2 and 4? Actually values: [1,2,3,2,3,2,1]; max=3 at index2 and index4. But between them index3=2, so non-contiguous. So L=2,R=4, but check that all between L and R are M fails because index3=2 !=3. So NO.
-
[1,2,3,2,2,1]? M=3, L=2,R=2. Before L: i=0:1<2, i=1:2<3 ok. After R: from i=2: a[2]>a[3]? 3>2 ok; i=3: a[3]>a[4]? 2>2? false. So NO.
-
[3,3,2,2,1]? This is constant then decreasing but with a constant segment then decreasing with a constant part? Actually [3,3,2,2,1] has two constant segments: constant 3, then decrease to 2, then constant 2, then decrease to 1. That's not allowed because after decreasing, we cannot have constant. Check: M=3, L=0,R=1. Before L: none. After R: need strictly decreasing from R to end. Check a[1]>a[2]? 3>2 ok; a[2]>a[3]? 2>2? false (equal). So fails. So NO.
-
[1,2,2,2,3]? M=3, L=4,R=4. Before L: need strictly increasing up to index4. Check i=0:1<2 ok; i=1:2<2? false. So NO.
-
[1,2,3,3,3]? M=3, L=2,R=4. Before L: i=0:1<2, i=1:2<3 ok. After R: R is last index, so no check. All between L,R are 3. So YES. That's increase then constant, no decrease.
-
[3,3,3,2,1]? M=3, L=0,R=2. Before L: none. After R: i=2:3>2 ok; i=3:2>1 ok. All between L,R are 3. So YES. That's constant then decrease.
Thus algorithm is solid.
Now, implement in C++. Input size small, so any method works.
Implementation steps:
- Read n.
- If n==1: print "YES" and return.
- Read array a of size n.
- Find max value M.
- Find first index L: smallest i such that a[i]==M.
- Find last index R: largest i such that a[i]==M.
- Check that for all i from 0 to L-1: if i+1 < n, we need a[i] < a[i+1] (strict increase). Actually need to check all consecutive pairs from start to L inclusive? Since we require strictly increasing up to L, meaning a[0] < a[1] < ... < a[L]. So we can loop i from 0 to L-1 and check a[i] < a[i+1]. If L==0, skip.
- Check that for all i from R to n-2: a[i] > a[i+1] (strict decrease from R to end). If R == n-1, skip.
- Check that for all i from L to R: a[i] == M. This ensures that all elements in the max block are M. Actually if we have L and R as first and last occurrence of M, it's possible that some element between L and R is not M if M appears non-contiguously? But if we find L as first occurrence and R as last occurrence, and we check that all between are M, then we ensure contiguity. Alternatively, we can check that there is no i such that a[i] != M but a[i] is surrounded by M? But easier: loop from L to R and verify all equal M.
- If all checks pass, print "YES", else "NO".
Note: The condition that the max block is contiguous also implicitly ensures that there is no other M later after a lower element. That's covered.
Edge case: n=0? Not possible.
Now, what about arrays with multiple equal maximum but not contiguous? Already covered.
Thus we have a simple solution.
Let's test with a case where maximum appears contiguously but the increasing or decreasing conditions fail.
All good.
Now code in C++.
One nuance: If there are multiple equal maxima but there is a dip inside? Actually if there is a dip, then the maximum appears non-contiguously, as the dip is lower than M. So our L and R will span indices that include the dip? No, L is first occurrence of M, R is last occurrence of M. If there is a dip between them, that index would have value less than M, so it would not be counted as M. But L and R are defined as first and last indices where a[i]==M. So if there is a dip, say indices: 0:M, 1:M, 2: lower, 3:M, then L=0, R=3. But index2 is not M, so when we check all from L to R are M, we will see a[2] != M, so fail. So correct.
Also need to ensure that before L, there is no equality or decrease. Our strict increase check ensures that.
Now, is there any case where the array is unimodal but our checks might fail due to first and last occurrences not being the correct peak segment? For example, if there are multiple peaks but same maximum? Not possible because maximum is the largest value; any other peak would be lower.
Ok.
Now implement.
Pseudo-code:
codeint n; cin >> n; vector<int> a(n); for (int i=0; i<n; i++) cin >> a[i]; if (n == 1) { cout << "YES" << endl; return 0; } int M = *max_element(a.begin(), a.end()); int L = -1, R = -1; for (int i=0; i<n; i++) { if (a[i] == M) { if (L == -1) L = i; R = i; } } // Check contiguity bool ok = true; for (int i = L; i <= R; i++) { if (a[i] != M) { ok = false; break; } } if (!ok) { cout << "NO" << endl; return 0; } // Check increasing before L for (int i = 0; i < L; i++) { if (a[i] >= a[i+1]) { ok = false; break; } // must be strictly increasing } if (!ok) { cout << "NO" << endl; return 0; } // Check decreasing after R for (int i = R; i < n-1; i++) { if (a[i] <= a[i+1]) { ok = false; break; } // must be strictly decreasing } if (ok) cout << "YES" << endl; else cout << "NO" << endl;Double-check edge cases:
-
n=2, a=[2,2]: M=2, L=0,R=1. Contiguity check: indices 0 and 1 are both 2, ok. Before L: i<0, skip. After R: i from 1 to 0? i=R=1, n-1=1 => i < n-1? condition i < n-1, so for n=2, n-1=1, so i<1 false, so skip. So OK => YES. Good.
-
n=2, a=[2,1]: M=2, L=0,R=0. Contiguity: just index 0, ok. Before L: skip. After R: i from 0 to 0? i=R=0, n-1=1, i < n-1 true, so check a[0] > a[1]? 2>1 true => OK => YES.
-
n=2, a=[1,2]: M=2, L=1,R=1. Contiguity ok. Before L: i from 0 to 0 check a[0] < a[1]? 1<2 true => OK. After R: skip. => YES.
-
n=2, a=[1,1]: M=1, L=0,R=1. Contiguity ok. Before L skip. After R skip. => YES.
-
n=3, a=[1,2,1]: M=2, L=1,R=1. Before L: i=0 check a[0] < a[1]? 1<2 true. After R: i=1 check a[1] > a[2]? 2>1 true. => YES. That's typical peak.
-
n=3, a=[1,1,2]: M=2, L=2,R=2. Before L: i=0 check a[0] < a[1]? 1<1 false => NO. So not unimodal (constant then increase). Good.
-
n=3, a=[2,1,1]: M=2, L=0,R=0. After R: i=0 check a[0] > a[1]? 2>1 true; i=1 check a[1] > a[2]? 1>1 false => NO. So not unimodal (decrease then constant). Good.
-
n=3, a=[2,2,1]: M=2, L=0,R=1. Contiguity: indices 0,1 are 2,2 ok. Before L skip. After R: i=1 to 1? i=R=1, n-1=2, i < n-1 true, so check a[1] > a[2]? 2>1 true. => YES. That's constant then decrease. Good.
-
n=3, a=[1,2,2]: M=2, L=1,R=2. Before L: i=0 check a[0] < a[1]? 1<2 true. After R: R=2, n-1=2, i< n-1 false, skip. Contiguity: indices 1,2 both 2 ok. => YES. That's increase then constant.
-
n=3, a=[1,2,3]: M=3, L=2,R=2. Before L: i=0 check a[0] < a[1]? 1<2 true; i=1 check a[1] < a[2]? 2<3 true. After R skip. => YES. That's strictly increasing.
-
n=3, a=[3,2,1]: as before YES.
-
n=3, a=[1,3,2]: M=3, L=1,R=1. Before L: i=0 check a[0] < a[1]? 1<3 true. After R: i=1 check a[1] > a[2]? 3>2 true. => YES. That's increase then decrease (peak at middle). Good.
-
n=3, a=[2,1,2]: M=2, L=0,R=2? Actually max=2 appears at indices 0 and 2. L=0,R=2. Check contiguity: from 0 to 2, a[1]=1 !=2, so fail => NO. This is valley then peak, not unimodal.
All good.
Now let's consider if there is a case where there are multiple equal maximum but the increasing part is not strictly increasing because of equalities before the first max? That would be caught by the before check because we require a[i] < a[i+1]. So if there is an equality before L, e.g., [2,2,3], L=2, before L: i=0 check a[0] < a[1]? 2<2 false, so NO.
What about [1,2,2,2,1]? M=2, L=1,R=3. Before L: i=0 check 1<2 true. After R: i=3 check a[3] > a[4]? 2>1 true. contiguity ok. => YES. That's increase, constant, decrease.
What about [1,2,2,3,2,1]? M=3, L=3,R=3. Before L: i=0 check 1<2 true; i=1 check 2<2 false => NO.
What about [2,2,1,1]? M=2, L=0,R=1. After R: i=1 check a[1] > a[2]? 2>1 true; i=2 check a[2] > a[3]? 1>1 false => NO.
Thus seems correct.
Now, about output: uppercase "YES" or "NO". We'll output accordingly.
Potential pitfalls:
- When L=0 and R=n-1 (all elements equal), both loops skipped, contiguity check passes, answer YES.
- When L=0 and R < n-1, we need to check decreasing from R to end. Good.
- When L>0 and R=n-1, we need to check increasing before L.
- When L>0 and R < n-1, both checks.
Now confirm that the condition for increasing before L includes the step from L-1 to L. That's checked because loop i goes from 0 to L-1, and we check a[i] < a[i+1]. When i = L-1, we check a[L-1] < a[L], which ensures the last step before the max is strictly increasing.
Similarly, for decreasing after R, loop i from R to n-2, includes i=R checking a[R] > a[R+1], ensuring immediate drop after the max block.
Now consider case where max block has length >1 and the value after R might equal M? But R is last occurrence of M, so a[R+1] if exists is < M, because if it were equal, then R wouldn't be last occurrence. So a[R] > a[R+1] is required by strict decreasing? Actually we need strictly decreasing after the block, so a[R] (which is M) must be > a[R+1]. Since a[R+1] < M, that's fine. But we also need a[R+1] > a[R+2] > ... . Our loop checks those.
Now consider if there is a situation where after R, we have a value equal to M? Not possible because R is last occurrence.
Now, what about values before L that equal M? Since L is first occurrence of M, all indices before L have a[i] < M. So the increasing condition will ensure they are strictly increasing up to M.
Thus algorithm is correct.
Now, time to produce the final answer in C++.
But the problem statement says n up to 100, so it's trivial.
Now, we should also consider alternative approach: using state machine. But the above is simpler.
Thus final code:
cpp#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; if (n == 1) { cout << "YES" << endl; return 0; } int M = *max_element(a.begin(), a.end()); int L = -1, R = -1; for (int i = 0; i < n; ++i) { if (a[i] == M) { if (L == -1) L = i; R = i; } } // check contiguity of max segment for (int i = L; i <= R; ++i) { if (a[i] != M) { cout << "NO" << endl; return 0; } } // check strictly increasing before L for (int i = 0; i < L; ++i) { if (a[i] >= a[i+1]) { cout << "NO" << endl; return 0; } } // check strictly decreasing after R for (int i = R; i < n-1; ++i) { if (a[i] <= a[i+1]) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; return 0; }Test with given examples:
Example 1: 6; 1 5 5 5 4 2 -> M=5, L=1,R=3, contiguity ok; before L: i=0: 1<5 ok; after R: i=3:5>4 ok; i=4:4>2 ok; => YES.
Example 2: 5; 10 20 30 20 10 -> M=30, L=2,R=2; before L: i=0:10<20 ok; i=1:20<30 ok; after R: i=2:30>20 ok; i=3:20>10 ok => YES.
Example 3: 4; 1 2 1 2 -> M=2, L=1,R=3? Actually a[0]=1, a[1]=2, a[2]=1, a[3]=2; max=2; first L=1, last R=3. contiguity check: i from 1 to 3: a[1]=2, a[2]=1 -> fail -> NO. Good.
Example 4: 7; 3 3 3 3 3 3 3 -> M=3, L=0,R=6; contiguity ok; before L none; after R none => YES.
Edge: n=1 -> YES.
All good.
Now produce final answer. ```cpp #include #include #include using namespace std;
int main() { int n; cin >> n; vector a(n); for (int i = 0; i < n; ++i) cin >> a[i];
code// A single element is always unimodal. if (n == 1) { cout << "YES\n"; return 0; } // Find the maximum value. int M = *max_element(a.begin(), a.end()); // Locate first and last occurrence of the maximum. int L = -1, R = -1; for (int i = 0; i < n; ++i) { if (a[i] == M) { if (L == -1) L = i; R = i; } } // All elements between L and R must be exactly M (contiguous block). for (int i = L; i <= R; ++i) { if (a[i] != M) { cout << "NO\n"; return 0; } } // Strictly increasing from the start up to L. for (int i = 0; i < L; ++i) { if (a[i] >= a[i + 1]) { cout << "NO\n"; return 0; } } // Strictly decreasing from R to the end. for (int i = R; i < n - 1; ++i) { if (a[i] <= a[i + 1]) { cout << "NO\n"; return 0; } } cout << "YES\n"; return 0;}
code
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →