山东省第九届ACM E-Sequence

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

题目大意:好数:在该数前面有比它小的数为好数(下标和值都比它小),要求删除一个数,求删除后好数最多的数,若有多个,输出最小的那个。

思路:跑一边维护最小值与次小值。

代码如下:

#include <bits/stdc++.h>
using namespace std;
const int MAXN=1000005;
int cnt[MAXN];
int main()
{
    int t;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            int n; scanf("%d",&n);
            int min1=MAXN,min2=MAXN;
            for(int i=0;i<n;i++)
            { // cnt[i] 表示删除i后会减少的good数的数量
                int x; scanf("%d",&x); cnt[x]=0;
                if(min1<x && x<min2) cnt[min1]++; 
                // x确定是因min1而成为一个good数
                // 删除min1 就会少了x这个good数 
                if(min1<x) cnt[x]++;
                // x确定是一个good数 
                // 删除x 至少会少了其本身这个good数
                if(x<min1) min2=min1,min1=x;
                else if(x<min2) min2=x;
            }
            int mini=MAXN,ans;
            for(int i=1;i<=n;i++)
            { // 选出cnt[i]最小的即可
                if(cnt[i]==mini) ans=min(ans,i);
                else if(cnt[i]<mini) mini=cnt[i], ans=i;
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值