Remove Extra One Codeforces Round #450 (Div. 2)(线段树)

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.

题意:

删除一个数使得record的个数最大;

思路:

用了个比较麻烦的想法,维护了两个线段树。。。我们知道只有左边比当前位置大的数只有一个的时候,我们才有可能去考虑他,所以我们把这些只有一个的数比他大都保存起来,然后枚举删哪个数,根据能增加的价值去更新。。还有一些细节,看代码吧

#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f
#define lson  rt<<1
#define rson rt<<1|1
using namespace std;
const int maxn =  1e5+5;
int sum[maxn<<2];
int sum2[maxn<<2];
int a[maxn];
int n;
void push_up(int rt)
{
    sum[rt] = sum[lson] +sum[rson];
    sum2[rt] = sum2[lson]+sum2[rson];
}
int query(int l,int r,int L,int R,int rt)
{
    if(L<=l&&R>=r){
        return sum[rt];
    }
    int mid = (l+r)>>1;
    int ret = 0;
    if(L<=mid){
        ret += query(l,mid,L,R,lson);
    }
    if(mid<R){
        ret += query(mid+1,r,L,R,rson);
    }
    return ret;
}
int query2(int l,int r,int L,int R,int rt)
{
    if(L<=l&&R>=r){
        return sum2[rt];
    }
    int mid = (l+r)>>1;
    int ret = 0;
    if(L<=mid){
        ret += query2(l,mid,L,R,lson);
    }
    if(mid<R){
        ret += query2(mid+1,r,L,R,rson);
    }
    return ret;
}
void update(int pos,int va,int l,int r,int rt)
{
    if(l==r&&r==pos)
    {
        sum[rt]+= va;
        return ;
    }
    int mid  = (l+r)>>1;
    if(pos<=mid){
        update(pos,va,l,mid,lson);
    }
    else{
        update(pos,va,mid+1,r,rson);
    }
    push_up(rt);
}
void update2(int pos,int va,int l,int r,int rt)
{
    if(l==r&&r==pos)
    {
        sum2[rt]+= va;
        return ;
    }
    int mid  = (l+r)>>1;
    if(pos<=mid){
        update2(pos,va,l,mid,lson);
    }
    else{
        update2(pos,va,mid+1,r,rson);
    }
    push_up(rt);
}
void update3(int pos,int l,int r,int rt)
{
    if(l==r&&r==pos)
    {
        sum2[rt] = 0;
        return ;
    }
    int mid  = (l+r)>>1;
    if(pos<=mid){
        update3(pos,l,mid,lson);
    }
    else{
        update3(pos,mid+1,r,rson);
    }
    push_up(rt);
}
int vis[maxn];
int main(){
    while(~scanf("%d",&n))
    {
        memset(vis,0,sizeof(vis));
        memset(sum2,0,sizeof(sum2));
        memset(sum,0,sizeof(sum));
        int mx = -1;
        int ans;
        for(int i = 1;i<=n;i++){
            scanf("%d",&a[i]);
            int tmp = query(1,n,a[i]>=n?a[i]:a[i]+1,n,1);
          //  cout<<tmp<<' '<<i<<endl;
            if(tmp==1){
                update2(a[i],1,1,n,1);
            }
            else if(tmp==0){
                vis[i] = 1;
            }
            update(a[i],1,1,n,1);
        }
        for(int i = 1;i<=n;i++)
        {int tmp;
            if(vis[i]==0){
                tmp = 0;
            }
            else{

            if(a[i]==1){
                tmp = 0;
            }
            else
                tmp = query2(1,n,1,a[i]-1,1);
              //  cout<<tmp<<' ';
            tmp-=vis[i]?1:0;
            }
          //  cout<<tmp<<' '<<a[i]<<endl;
            if(tmp>mx){
                mx =tmp;
                ans = a[i];

            }
            else if(tmp==mx){
                ans = min(ans,a[i]);
            }
            update3(a[i],1,n,1);
        }
        cout<<ans<<endl;
    }
}


数据治理是确保数据准确性、可靠性、安全性、可用性和完整性的体系和框架。它定义了组织内部如何使用、存储、保护和共享数据的规则和流程。数据治理的重要性随着数字化转型的加速而日益凸显,它能够提高决策效率、增强业务竞争力、降低风险,并促进业务创新。有效的数据治理体系可以确保数据在采集、存储、处理、共享和保护等环节的合规性和有效性。 数据质量管理是数据治理中的关键环节,它涉及数据质量评估、数据清洗、标准化和监控。高质量的数据能够提升业务决策的准确性,优化业务流程,并挖掘潜在的商业价值。随着大数据和人工智能技术的发展,数据质量管理在确保数据准确性和可靠性方面的作用愈发重要。企业需要建立完善的数据质量管理和校验机制,并通过数据清洗和标准化提高数据质量。 数据安全与隐私保护是数据治理中的另一个重要领域。随着数据量的快速增长和互联网技术的迅速发展,数据安全与隐私保护面临前所未有的挑战。企业需要加强数据安全与隐私保护的法律法规和技术手段,采用数据加密、脱敏和备份恢复等技术手段,以及加强培训和教育,提高安全意识和技能水平。 数据流程管理与监控是确保数据质量、提高数据利用率、保护数据安全的重要环节。有效的数据流程管理可以确保数据流程的合规性和高效性,而实时监控则有助于及时发现并解决潜在问题。企业需要设计合理的数据流程架构,制定详细的数据管理流程规范,并运用数据审计和可视化技术手段进行监控。 数据资产管理是将数据视为组织的重要资产,通过有效的管理和利用,为组织带来经济价值。数据资产管理涵盖数据的整个生命周期,包括数据的创建、存储、处理、共享、使用和保护。它面临的挑战包括数据量的快速增长、数据类型的多样化和数据更新的迅速性。组织需要建立完善的数据管理体系,提高数据处理和分析能力,以应对这些挑战。同时,数据资产的分类与评估、共享与使用规范也是数据资产管理的重要组成部分,需要制定合理的标准和规范,确保数据共享的安全性和隐私保护,以及建立合理的利益分配和权益保障机制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值