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.

题意:

如果一个数比前面的数都大,那么就产生一个贡献

现在要你去掉一个数,使得剩下数字串总贡献最大,求这个数

如果几个数字一样,输出较大的

题解:

这题似乎可以用树状数组手糊,但标算更加高妙

因为最大值可以删去,所以我们关心的不仅只有最大值,还有次大的

一组数字,没有修改的话原本的贡献是相同的,所以问题就变成了去掉一个数最多能增加多少贡献

这该怎么记录呢?

如果有一个数比当前最大数大,那么去掉它会产生负贡献

如果比最大值大,比次大值小,那么去掉最大值会增加一个贡献

所以建一个cnt数组,cnt[i]表示去掉其所增加的贡献

扫一遍取最大值即可

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int n,a[],cnt[]; int main()
{
int max1=,max2=;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=;i<=n;i++)
{
if(a[i]>max1)
{
max2=max1;
max1=a[i];
cnt[a[i]]--;
}
else
{
if(a[i]>max2)
{
cnt[max1]++;
max2=a[i];
}
}
}
int ans,max3=-;
for(int i=;i<=n;i++)
{
if(cnt[i]>max3)
{
max3=cnt[i];
ans=i;
}
}
printf("%d\n",ans);
}

 

Codeforces 900C. Remove Extra One(暴力)的更多相关文章

  1. Codeforces 900C Remove Extra One 模拟

    题目链接:900C Remove Extra One 题意: 首先record是指这个数比数列前面的所有数都大,给了n个数(1-n),删掉一个数,让整个数列的record值达到最大. 题解: 刚开始我 ...

  2. Codeforces Round #450 (Div. 2) C. Remove Extra One【*模拟链表/一个数比前面所有数大就是个record。删掉一个数,让record的个数尽量多。】

    C. Remove Extra One time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. [CodeForces - 1272D] Remove One Element 【线性dp】

    [CodeForces - 1272D] Remove One Element [线性dp] 标签:题解 codeforces题解 dp 线性dp 题目描述 Time limit 2000 ms Me ...

  4. Codeforces Round #450 (Div. 2) C. Remove Extra One

    题目链接 题意:让你去掉一个数,使得剩下的数的record最多,当1≤j<i的aj<ai1 \leq j< i的a_j<a_i1≤j<i的aj​<ai​时aia_i ...

  5. 【Codeforces Round #450 (Div. 2) C】Remove Extra One

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举删除第i个数字. 想想删掉这个数字后会有什么影响? 首先,如果a[i]如果是a[1..i]中最大的数字 那么record会减少1 ...

  6. Codeforces Gym 100015H Hidden Code 暴力

    Hidden Code 题目连接: http://codeforces.com/gym/100015/attachments Description It's time to put your hac ...

  7. Codeforces gym 100685 A. Ariel 暴力

    A. ArielTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/A Desc ...

  8. Codeforces Gym 100637G G. #TheDress 暴力

    G. #TheDress Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/G ...

  9. [ An Ac a Day ^_^ ] CodeForces 691F Couple Cover 花式暴力

    Couple Cover Time Limit: 3000MS   Memory Limit: 524288KB   64bit IO Format: %I64d & %I64u Descri ...

随机推荐

  1. VCS (版本控制系统)

    1.什么是VCS? 版本控制系统(version control system),是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统.版本控制系统不仅可以应用于软件源代码的文本文件, ...

  2. ubuntu为什么没有/etc/inittab文件? 深究ubuntu的启动流程分析

    Linux 内核启动 init ,init进程ID是1,是所有进程的父进程,所有进程由它控制. Ubuntu 的启动由upstart控制,自9.10后不再使用/etc/event.d目录的配置文件,改 ...

  3. (转)Oracle中动态SQL详解

    本文转载自:http://www.cnblogs.com/gaolonglong/archive/2011/05/31/2064790.html 1.静态SQLSQL与动态SQL Oracle编译PL ...

  4. 1041 Be Unique

    题意:找到一串数字序列中首个出现的不重复的数字. 思路:用哈希,因为数值大小在[1,10^4],所以可以直接开数组.输入数据时记录每个数字出现过的次数.然后遍历原序列,遇到第一个次数为1的数字就是所求 ...

  5. AJAX的流程是什么?

    客户端产生js的事件 创建XMLHttpRequest对象 对XMLHttpRequest进行配置 通过AJAX引擎发送异步请求 服务器端接受请求并且处理请求,返回html或者xml内容 XML调用一 ...

  6. jqurey datatable tableTools 自定义button元素 以及按钮定义事件

    版本 1.10.4 "dom": 'T<"clear">lfrtip', "tableTools": { //"sSw ...

  7. java5 Condition用法--实现线程间的通信

    Condition的功能类似在传统线程技术中的Object.wait()和Object.natify()的功能,传统线程技术实现的互斥只能一个线程单独干,不能说这个线程干完了通知另一个线程来干,Con ...

  8. libevent源码深度剖析四

    libevent源码深度剖析四 ——libevent源代码文件组织 1 前言 详细分析源代码之前,如果能对其代码文件的基本结构有个大概的认识和分类,对于代码的分析将是大有裨益的.本节内容不多,我想并不 ...

  9. laravel策略类,实现当前登陆的用户是否具有删除,修改文章的权限

    策略类依赖月门脸类Auth 首先创建一个门脸类 make:auth 然后再创建一个策略  php artisan make:policy PostPolicy 定义Auth的登陆类,用的是哪个模型登陆 ...

  10. iframe 模拟ajax文件上传and formdata ajax 文件上传

    对于文件上传 有好多种方式,一直想总结 文件上传的方法 今天就来写下 iframe  的文件上传的代码 本人语言表达能里有限,不多说了 直接上代码. 首先看 总体页面. 总共就三个文件. 实际上也就是 ...