codeforces 900C. Remove Extra One

来源:互联网 发布:群发qq消息软件 编辑:程序博客网 时间:2024/05/22 06:23

题意:给你一个n,下面有一个1~n的排列,当a[i]比他前面所有的数字都大时,那么a[i]就是一个记录数,问删掉哪一个数字可以使这个序列的记录数最多?如果两个数字一样

思路:统计出来每一个数字删掉以后能使后面多出来几个记录数,选一个最大的就行。怎么统计呢?我们需要维护两个值,一个最大值max1,一个次大值max2,因为对于当前数字a[i]有三种情况:①a[i] > max1,那么删掉它将会使记录数个数减少一。②max2 < a[i] < max1,删掉a[i]没什么用,因为前面有max1,而删掉max1,会使记录数个数增加一。③a[i] < max2,删掉a[i]对记录数没有影响。所以我们只需要扫一遍,用cnt[a[i]]存储下来删掉a[i]后使记录数增加的值,然后挑一个最大值出来就可以了。

#include<bits/stdc++.h>using namespace std;const int MAXN = 1e5 + 5;int n, a[MAXN], cnt[MAXN];int main(){while(~scanf("%d",&n)){memset(cnt,0,sizeof(cnt));int max1 = 0, max2 = 0;//最大和次大 for(int i = 1; i <= n; i++){scanf("%d",&a[i]);if(max1 < a[i]) max2 = max1, max1 = a[i], cnt[a[i]]--;        else if(max2 < a[i]) cnt[max1]++, max2 = a[i];}int ans = 1;     for(int i = 2; i <= n; i++)    {    if(cnt[ans] < cnt[i]) ans = i;}printf("%d\n",ans);}return 0;}/*1155 1 2 3 4*/


原创粉丝点击