第九届山东省ACM大学生程序设计竞赛Sequence +思维

本文解析了一道ACM竞赛中的序列优化问题,通过定义“好”元素并给出算法实现,旨在找到移除哪个元素能使序列中“好”元素数量最大化。提供了完整的C++代码实现。
链接: https://www.nowcoder.com/acm/contest/123/E
来源:牛客网

时间限制:C/C++ 5秒,其他语言10秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

We define an element  in a sequence "good", if and only if there exists  (1≤j<i) such that .

Given a permutation p of integers from 1 to n. Remove an element from the permutation such that the number of "good" elements is maximized.

输入描述:

The input consists of several test cases. The first
line of the input gives the number of test cases,.

For each test case, the first line contains an
integer ,
representing the length of the given permutation.

The second line contains n integersp1,p2,…,pn ,
representing the given permutation p.

It's guaranteed that  .

输出描述:

For each test case, output one integer in a single
line, representing the element that should be deleted. If there are several
answers, output the minimal one.
示例1

输入

2
1
1
5
5 1 2 3 4

输出

1
5
#define happy

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define all(a) (a).begin(),(a).end()
#define pll pair<ll,ll>
#define vi vector<int>
#define P pair<int,int>
#define pb push_back
const int inf=0x3f3f3f3f;
ll rd(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
const int N=1e6+10;
ll ft[N],sd[N],a[N];
int cnt[N];
bool flag[N];
int main(){
#ifdef happy
    freopen("in.txt","r",stdin);
#endif
    int t=rd();
    while(t--){
        ll now=inf,pre=inf;
        ll n=rd();
        rep(i,1,n){
            a[i]=rd();
            ft[i]=now,sd[i]=pre;
            if(a[i]<=now){pre=now;now=a[i];}
            else if(a[i]<pre)pre=a[i];
        }
        rep(i,1,n){
            if(a[i]<=ft[i])flag[i]=1;
            else flag[i]=0;
        }
        ll k=0;
        rep(i,1,n){
            if(flag[i]==1){
                k=i;
                cnt[k]=0;
            }else if(a[i]<=sd[i])
            cnt[k]++;
        }
        ll ans=inf;
        rep(i,1,n)if(flag[i]==1&&cnt[i]==0)ans=min(ans,a[i]);
        if(ans==inf){
            rep(i,1,n){
                if(flag[i]==0)ans=min(ans,a[i]);
                else if(cnt[i]==1)ans=min(ans,a[i]);
            }
        }
        printf("%lld\n",ans);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值