【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
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 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/439705.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

一步步编写操作系统 45 用c语言编写内核2

在linux下用于链接的程序是ld&#xff0c;链接有一个好处&#xff0c;可以指定最终生成的可执行文件的起始虚拟地址。它是用-Ttext参数来指定的&#xff0c;所以咱们可以执行以下命令完成链接&#xff1a; ld kernel/main.o -Ttext 0xc0001500 -e main -o kernel/kernel.bin …

【Codeforces - 977F】Consecutive Subsequence(STLmap,输出路径,dp)

题干&#xff1a; You are given an integer array of length nn. You have to choose some subsequence of this array of maximum length such that this subsequence forms a increasing sequence of consecutive integers. In other words the required sequence should …

使用OpenCV库快速求解相机内参

本文主要介绍如何使用OpenCV库函数求解相机内参。具体可查阅官网&#xff1a;https://docs.opencv.org/master/dc/dbb/tutorial_py_calibration.html。 关于相机内参的求解还有很多其它的工具&#xff0c;如使用MATLAB求解会更方便&#xff0c;直接调用MATLAB中的APP即可。 1.背…

一步步编写操作系统 46 用c语言编写内核3

再把上节代码贴出来&#xff0c; 1 //int main(void) { 2 int _start(void) { 3 while(1); 4 return 0; 5 }有没有同学想过&#xff0c;这里写一个_start函数&#xff0c;让其调用main函数如何&#xff1f;其实这是可以的&#xff0c;main函数并不是第一个函数&#xff0c;它实…

从零实现一个3D目标检测算法(2):点云数据预处理

在上一篇文章《从零实现一个3D目标检测算法&#xff08;1&#xff09;&#xff1a;3D目标检测概述》对3D目标检测研究现状和PointPillars模型进行了介绍&#xff0c;在本文中我们开始写代码一步步实现PointPillars&#xff0c;这里我们先实现如何对点云数据进行预处理。 在图像…

【CodeForces - 129C】Statues(思维,bfs)

题干&#xff1a; In this task Anna and Maria play a game with a very unpleasant rival. Anna and Maria are in the opposite squares of a chessboard (8  8): Anna is in the upper right corner, and Maria is in the lower left one. Apart from them, the board h…

一步步编写操作系统 47 48 二进制程序运行方式

操作系统并不是在功能上给予用户的支持&#xff0c;这种支持是体现在机制上。也就是说&#xff0c;单纯的操作系统&#xff0c;用户拿它什么都做不了&#xff0c;用户需要的是某种功能。而操作系统仅仅是个提供支持的平台。 虽然我们是模仿linux来写一个黑屏白字的系统&#x…

百度顶会论文复现(1):课程概述

最近百度推出了一款重磅课程《全球顶会论文作者&#xff0c;28天免费手把手带你复现顶会论文》。这个课程真的是很硬核的课程&#xff0c;这里简单记录下自己的学习过程。 文章目录1. 课程设计思路和安排2. 课程大纲1. 课程设计思路和安排 课程设计思路如下&#xff0c;共分为…

【Codeforces - 127D】Password(思维,二分+字符串Hash)

题干&#xff1a; Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock be…

百度顶会论文复现(2):GAN综述

本节课主要是对GAN的发展进行了介绍&#xff0c;包括基本原理&#xff0c;训练方法&#xff0c;存在问题&#xff0c;改进以及应用场景等。实践作业则为手写数字生成。课程地址为&#xff1a;https://aistudio.baidu.com/aistudio/education/preview/493290。 文章目录1.什么是…

一步步编写操作系统 48 二进制程序的加载方式

接上节&#xff0c;程序头可以自定义&#xff0c;只要我们按照自己定义的格式去解析就行。也许我光这么一说&#xff0c;很多同学还是不能彻底明白如何自定义文件头&#xff0c;因为大多数同学都是用高级语言来写程序&#xff0c;即使用了偏底层的c语言&#xff0c;不同平台的c…

【Codeforces - 864D】Make a Permutation!(贪心,字典序)

题干&#xff1a; Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n. Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his arra…

百度顶会论文复现(3):视频分类综述

本节课主要是对视频分类的发展进行了介绍&#xff0c;包括任务与背景&#xff0c;分类方法&#xff0c;前沿进展等。课程地址为&#xff1a;https://aistudio.baidu.com/aistudio/course/introduce/1340?directly1&shared1。 文章目录1. 任务与背景2. 视频分类方法2.1 双流…

一步步编写操作系统 46 linux的elf可执行文件格式1

ELF文件格式依然是分为文件头和文件体两部分&#xff0c;只是该文件头相对稍显复杂&#xff0c;类似层次化结构&#xff0c;先用个ELF header从“全局上”给出程序文件的组织结构&#xff0c;概要出程序中其它头表的位置大小等信息&#xff0c;如程序头表的大小及位置、节头表的…

百度顶会论文复现(4):飞桨API详解

本节课主要是对飞桨常用API进行了介绍&#xff0c;课程地址为&#xff1a;https://aistudio.baidu.com/aistudio/education/group/info/1340。 文章目录1.飞桨API官网2. API使用介绍3. 飞桨模型操作1.飞桨API官网 官网地址为&#xff1a;https://www.paddlepaddle.org.cn/docu…

【Codeforces - 977D】Divide by three, multiply by two(思维构造)

题干&#xff1a; Polycarp likes to play with numbers. He takes some integer number xx, writes it down on the board, and then performs with it n−1n−1 operations of the two kinds: divide the number xx by 33 (xx must be divisible by 33);multiply the numbe…

一步步编写操作系统 45 linux的elf可执行文件中的段和节

接上文&#xff0c;为了描述清楚文件格式的本质&#xff0c;咱们先从最基本的“段”说起。 程序中最重要的部分就是段&#xff08;segment&#xff09;和节&#xff08;section&#xff09;&#xff0c;它们是真正的程序体&#xff0c;是真真切切的程序资源&#xff0c;所以下…

视觉SLAM十四讲(3):三维空间刚体运动

本章需要掌握的知识点有&#xff1a;旋转矩阵&#xff0c;变换矩阵&#xff0c;四元数&#xff0c;欧拉角定义和数学表达&#xff1b;同时也要掌握Eigen库关于矩阵、几何模块的使用方法。 文章目录3.1 旋转矩阵3.1.1 点&#xff0c;向量和矩阵的关系3.1.2 坐标系间的欧式变换3.…

【CodeForces - 483C】Diverse Permutation(思维构造)

题干&#xff1a; Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of ndistinct positive integers not larger than n. Well denote as n the length of permutation p1,   p2,   ...,   pn. Your task is to find such…

一步步编写操作系统 47 elf格式文件分析实验

在上一节中&#xff0c;我们讲述了elf格式的部分理论知识&#xff0c;为什么是部分呢&#xff1f;因为我们本着“够用”的原则&#xff0c;只把我们需要了解的部分说完啦。不过&#xff0c;我相信大部分同学仅仅凭上一节中的理论知识还是领悟不到elf本质&#xff0c;咱们在本节…