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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【Codeforces - 900C】Remove Extra One(思维,STLset,tricks)

發布時間:2023/12/10 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Codeforces - 900C】Remove Extra One(思维,STLset,tricks) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

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.

題目大意:

給定一個長度為n的排列p。從排列中刪除一個元素,使“記錄”的數量盡可能多。

我們要提醒大家,在a1 a2…, ak中,元素ai是一個記錄,當且僅當對于每個整數j (1≤j < i),均需滿足aj < ai。

解題報告:

其實只需要統計出刪除某一個值后,會增加多少個“記錄”即可。

轉換主元,去考慮 j ,來維護前面的值,會發現只會對前面第二大的元素產生影響,所以對于每一個j,只會更新前面的一個值,set維護一下即可。

發現其實只用到了前兩大的元素,所以可以直接維護最大次大值,不難省掉set這個log。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; set<PII> ss; set<PII> ::iterator it,itt; int n,a[MAX],ok[MAX],ans[MAX]; int main() {cin>>n;for(int i = 1; i<=n; i++) scanf("%d",a+i);for(int i = 1; i<=n; i++) {it = ss.lower_bound(pm(a[i],-1)),itt=it;if(it == ss.end()) ok[i]=1;else {++itt;if(itt == ss.end()) ans[it->SS]++;}ss.insert(pm(a[i],i));}int mx=-1e7,mi=-1;for(int i = 1; i<=n; i++) {int tmp = ans[i] - ok[i];if(tmp > mx) {mx=tmp;mi = a[i];}else if (tmp == mx) mi = min(mi,a[i]);}printf("%d\n",mi);return 0 ; }

?

總結

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

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