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. python 数据库查询结果转对象

    #coding:utf-8 from json import dumps, loads, JSONEncoder, JSONDecoder import pickle from app.model.J ...

  2. resharper activate

    K03CHKJCFT-eyJsaWNlbnNlSWQiOiJLMDNDSEtKQ0ZUIiwibGljZW5zZWVOYW1lIjoibnNzIDEwMDEiLCJhc3NpZ25lZU5hbWUiO ...

  3. MAC 10.6 64wei

    苹果电脑 Mac OS X 10.6 雪豹系统同时支持 32 位和 64 位模式,关于开启 64 位的好处,字太多,本文后半段介绍.下面先说查看你的苹果电脑是否开启了 64 位以及设置苹果电脑 Mac ...

  4. 解决:An internal error occurred during: "Launching New_configuration". Path for project must have only one segment.

    问题: 点击运行时eclipse报错如下: An internal error occurred during: "Launching New_configuration". Pa ...

  5. mac下自定义伪协议配置

    之前查了很多资料,最近也在挖掘研究这方面的漏洞. windows的很简单,在注册表配置就好了,但是mac os 是unix的,没有注册表这么一说. 但是发现腾讯等配置了自定义等协议,例如:tencen ...

  6. Sublime Text 套件介紹:Pretty JSON

    JSON,一個輕量級的資料交換語言,目前許多網站AJAX request的回應結果都是JSON格式   以下是一個標準的JSON格式   1 2 3 4 5 6 7 8 9 10 11 12 13 1 ...

  7. peerconnection_client分析笔记

     Windows版本的peerconnection_client demo是一个win32程序,入口函数为main.cc里面的wWinMain,程序整体流程就从这个入口函数下手开始分析. 1.peer ...

  8. Unity3d平台信息设置

    [Unity3d平台信息设置] 通过"Edit" -> "Project Settings" -> "Player"菜单选项.打 ...

  9. as+bt=1是ab两数互质的充要条件

    [as+bt=1是ab两数互质的充要条件] 充分性,as+bt=1 => (a,b)=1: 因为as+bt=1,设c=(a,b),则c整除a和b,所以c整除as+bt,即c整除1,所以c=1,即 ...

  10. Linux VmWare安装虚拟机(centos6.9)

    开启虚拟机 ---------------------------------------------------------------------------------------------- ...