Remove Extra One(思维)
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible.
We remind that in a sequence of numbers a1,?a2,?…,?ak the element ai is a record if for every integer j (1?≤?j?<?i) the following holds: aj?<?ai.
Input
The first line contains the only integer n (1?≤?n?≤?105) — the length of the permutation.
The second line contains n integers p1,?p2,?…,?pn (1?≤?pi?≤?n) — the permutation. All the integers are distinct.
Output
Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one.
Examples
Input
1
1
Output
1
Input
5
5 1 2 3 4
Output
5
Note
In the first example the only element can be removed.
今天不適合寫題,各種wa。
題意:刪掉一個數字,使得這個序列中的record數最多。record序列是這樣定義的,如果一個數a[i],它在[1~i]之間是最大的。問這樣的數字最多的情況是刪除哪個數字之后才有的。
思路:這種題目,一般就是考慮刪除一個數字之后,對于整個序列的貢獻。如果這個數字是1~i最大的,那么刪除她對于這個序列來說就是少了一個。如果說這個數字是第二大的,那么刪除最大的數字之后,這個數字就成了最大了,那么刪除最大的數字對于整個序列的貢獻就是增加了一個。這樣我們就可以O(n)的時間復雜度處理出每個刪除數字的貢獻值,取最優的就可以了。
代碼如下:
努力加油a啊,(o)/~
總結
以上是生活随笔為你收集整理的Remove Extra One(思维)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: The World is a Theat
- 下一篇: Statues(三维bfs)