日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Remove Extra One(思维)

發布時間:2023/12/15 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)的時間復雜度處理出每個刪除數字的貢獻值,取最優的就可以了。
代碼如下:

#include <bits/stdc++.h> using namespace std;const int maxx=1e5+100; int n,x,_max,_max1; int dp[maxx];int main() {scanf("%d",&n);for(int i = 0; i < n; i ++){scanf("%d",&x);if(x>_max) dp[x]=1,_max1=_max,_max=x;//大于最大值就是最大值,記得更新次大值else if(x>_max1) dp[_max]--,_max1=x;}int ans = 1;for(int i=1;i<=n;i++){if(dp[i]<dp[ans]) ans=i;}printf("%d\n",ans);return 0; } /*7 1 6 7 4 2 5 3*/

努力加油a啊,(o)/~

總結

以上是生活随笔為你收集整理的Remove Extra One(思维)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。