【Codeforces 900C】Remove Extra One(BIT思维)

3 篇文章 0 订阅
2 篇文章 0 订阅
C. Remove Extra One
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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的排列,删除一个数使得rec数最多,rec数为这个数左边的数均比它小
思路:开始读错题了呀,以为rec数是左边单调递增的,诶诶。
           计算每个数对rec数的贡献,即这个数是唯一比它右边数大的,然后贡献最多的就是,用BIT维护了一个当前数是否为rec数或rec备选数。
           最后处理部分也搞得很蒙,写的乱七八糟。
           A了后搜了一下,发现只要维护一个最大数和次大数就可以,果然,我就是个zz
Code
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define maxn 100005
#define INF 0x3f3f3f3f
typedef long long ll;

using namespace std;

int n, c[maxn];
int lowbit(int x)
{
    return x & (-x);
}
void add(int k, int num)
{
    while(k <= n){
        c[k] += num;
        k += lowbit(k);
    }
}
int Sum(int k)
{
    int sum = 0;
    while(k){
        sum += c[k];
        k -= lowbit(k);
    }
    return sum;
}
int main()
{
    int k[maxn], a[maxn], rec[maxn];
    int fir;
    priority_queue<int, vector<int>, less<int> >q;
    while(~scanf("%d", &n)){
        memset(c, 0, sizeof(c));
        memset(k, 0, sizeof(k));
        memset(rec, 0, sizeof(rec));
        fir = 0;
        while(!q.empty()) q.pop();
        for (int i = 1; i <= n; i++){
            scanf("%d", &a[i]);
            if (!fir) fir = a[i];
            q.push(a[i]);
            add(a[i], 1);
            int num = Sum(a[i] - 1);
            if (num == i - 1) rec[a[i]] = 1;
            else if (num == i - 2) k[q.top()]++;
        }
        int mx = -1, t = -1;
        for (int i = 1; i <= n; i++){
            if (k[i] > mx){
                mx = k[i];
                t = i;
            }
        }
        if (mx == 1){
            if (t == fir || rec[t] == 1) t = 1;
        }
        if (mx == 0){
            for (int i = 1; i <= n; i++)
                if (i != a[i]){
                    t = i;
                    break;
                }
        }
        printf("%d\n", t);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值