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. 一键获取 所有连接过的WIFI密码

    使用方法 一.运行CMD (以及 开启无线网卡.最好是笔记本) 二.输入命令: for /f "skip=9 tokens=1,2 delims=:" %i in ('netsh ...

  2. QQ2008自动聊天精灵delphi源码

    QQ2008自动聊天精灵delphi源码   unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Grap ...

  3. win7系统清除USBSTOR记录

    方法一 1.Win+R,出现运行窗口,如图所示: 2.在输入框中输入“regedit”,如图所示: 3.进入后,点击编辑-查找,查找输入框中输入“USBSTOR”(为了加快查找速度,可以只选择“项”) ...

  4. 开学初的c

    cout<<a[i]<<" " 这个是先输出a[i]再输出空格 cout<<endl      这个是直接换行cout<<a[i]& ...

  5. git常用命令小结

    1.ssh连接方式 公钥生成ssh-keygen -t rsa -C "764432054@qq.com"在用户家目录下的.ssh目录下生成 id_rsa ,id_rsa.pub ...

  6. 【转】使用Jmeter对Websocket进行压力测试

    前段时间本着练习angularJS+requireJS的目的写了一个基于nodeJS和socket.io的聊天室,github地址为:https://github.com/towersxu/node- ...

  7. java代码----求最大值,平均值。。。

    总结:方法的返回值----返回的对象到底是什么? package com.a; import java.util.Scanner; //从键盘输入10个数,并输出最大值,最小值,平均值 public ...

  8. java代码 求和1+1/2+1/3+1/4+1/5+1/6+.......+1/n 的值~~~~

    总结:很简单的练习: s=1+1/2+1/3+1/4+1/5+1/6+.......+1/n的值:注意这里的s是float型,绝对记住不能留整数型 即s+=1/i; package com.c2; i ...

  9. 大杀器TheFatRat

    项目地址:https://github.com/Screetsec/TheFatRat 安装TheFatRat root@sch01ar:/sch01ar# git clone https://git ...

  10. WARNING: cell0 mapping not found - not syncing cell0

    WARNING: cell0 mapping not found - not syncing cell0