题干:

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)相关推荐

  1. Codeforces 1077B Disturbed People(思维题)

    Codeforces 1077B Disturbed People(思维题) There is a house with nn flats situated on the main street of ...

  2. Remove Extra One(思维)

    You are given a permutation p of length n. Remove one element from permutation to make the number of ...

  3. 【CodeForces - 558C】Amr and Chemistry(位运算,bfs,计数,思维,tricks)

    题干: Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting expe ...

  4. CodeForces - 160D Edges in MST(思维+tarjan/树链剖分+线段树)

    题目链接:点击查看 题目大意:给出一张 n 个点 m 条边组成的带权无向图,现在对于每条边来说,确定一下其分类: 一定是最小生成树上的边 可能是最小生成树上的边 一定不是最小生成树的边 题目分析:两种 ...

  5. Codeforces 846 A Curriculum Vitae 思维 暴力

    题目链接: http://codeforces.com/contest/846/problem/A 题目描述: 给你一个串, 你可以做删除操作, 要求结果串0不能在1的右边, 问最多可以剩几个数字 解 ...

  6. Codeforces 861 B Which floor? 思维

    题目链接: http://codeforces.com/contest/861/problem/B 题目描述: 每个楼梯上有相同数量的数, 都是从1开始, 给你m个不矛盾的信息, 问你能不能判断n在第 ...

  7. CodeForces - 1494E A-Z Graph(构造+思维)

    题目链接:https://vjudge.net/problem/CodeForces-1494E 题目大意:给出一个初始时只有 nnn 个点的有向带权图,需要执行 mmm 次操作,每次操作分为下列三种 ...

  8. CodeForces - 1504C Balance the Bits(思维+构造)

    题目链接:https://vjudge.net/problem/CodeForces-1504C 题目大意:给出一个长度为 nnn 的 010101 串,现在要求构造出两个长度为 nnn 的合法括号序 ...

  9. CodeForces - 594A Warrior and Archer(思维+博弈)

    题目链接:点击查看 题目大意:给出n个坐标轴上的点,两个人轮流操作,每次取走其中的一个点,直到最后剩余两个点为止,Vova先手,Vova希望两个点的距离尽可能小,Lesha希望两个点的距离尽可能大,问 ...

最新文章

  1. R语言构建多元线性回归模型
  2. JAXB--@XmlElementWrapper注解和泛型一起使用
  3. 一个中等规模的七段数码数据库以及利用它训练的识别网络
  4. jquery获取html代码怎么写,jQuery从html代码中获取对应标签的写法
  5. 小程序根据手机机型设置自定义底部导航距离
  6. java推送数据给安卓,java – 如何从Firebase推送通知中获取数据并将其显示在Android Activity中?...
  7. 【转】Linux下c++调用自己编写的matlab函数:通过mcc动态链接库.so实现
  8. PageRank 算法
  9. spring boot 调试 - 热部署
  10. Eureka 配置参数说明
  11. 透视宝移动端对Unity手机游戏引擎监控实现
  12. Java Design Pattern(Factory,Singleton,Prototype,Proxy)
  13. DOS控制台启动方式+DOS控制台常用命令
  14. SPSS安装多次显示请输入注册的电子邮箱时,采用的解决办法。亲测有效!
  15. C++的替代:微软如何使用rust?
  16. BilibiliVideoDownload for Mac(B站视频下载工具)
  17. 论文阅读笔记(Region Proposal by Guided Anchoring)
  18. 云南省昭通市领导一行莅临上海控安调研指导
  19. 关于FileWrite写入文本后,打开文本发现是空的
  20. Java(老白再次入门) - 多线程

热门文章

  1. 847. Shortest Path Visiting All Nodes(一)
  2. [剑指offer]面试题第[6]题[JAVA][旋转数组的最小数字][二分法]
  3. AtCoder-2379 - 连接竹竿 思维 | 数学
  4. html字体变大自动换行,网页css中实现字符超出宽度自动换行和英语字符不断行的解决方法...
  5. java getapplicationcontext_java – getApplicationContext()中的空指针
  6. kill 进程_结束进程,查看后台进程
  7. java写一个web聊天工具_javaweb写的在线聊天应用
  8. python读取图像数据流_浅谈TensorFlow中读取图像数据的三种方式
  9. python 线程池_老程序员的经验分享:Python 从业十年是种什么体验?
  10. mysqlworkbench导入sql文件_将sql脚本文件导入数据库