【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 1Output
1Input
5 5 1 2 3 4Output
5Note
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)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SSSvr.exe - SSSvr是什么
- 下一篇: 国内第四大运营商来了 中国广电5G今日正