2018山东省省赛 E.Sequence II

本文介绍了一种算法,用于解决给定排列中通过移除一个元素使“好”元素数量最大化的数学问题。“好”元素是指在序列中有较小值位于其左侧的元素。文章详细解释了算法思路,并附上了C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  

题目描述

We define an element ai in a sequence "good", if and only if there exists a j(1≤ j < i) such that aj < ai.
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, T(1≤ T≤ 10^3).
For each test case, the first line contains an integer n(1≤ n≤ 10^6), representing the length of the given permutation.
The second line contains n integers p1,p2,\cdots,pn(1≤ pi≤ n), representing  the given permutation p.
It’s guaranteed that Σn≤ 2× 10^7.
 

输出

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.

样例输入

2
1
1
5
5 1 2 3 4

样例输出

1
5

思路:计算每个数的贡献,即去掉该数后,有多少数会变成不是好数。
如何计算呢?维护一个最小前缀与次小前缀,如果后面一个数大于次小小于最小,那么这个将最小前缀的贡献加一。如果一个数本身是好数,这个数本身也要加1.
具体看代码:
#include <iostream>
#include <bits/stdc++.h>
#define maxn 1000005
using namespace std;
int a[maxn];
int in[maxn];
int read() {
    int x = 0;
    char c = getchar();
    while (c < '0' || c > '9')c = getchar();
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}
void write(int x) {
    int y = 10, len = 1;
    while (y <= x) {
        y *= 10;
        len++;
    }
    while (len--) {
        y /= 10;
        putchar(x / y + 48);
        x %= y;
    }
}
int main()
{
    int n,t,i;
    t=read();
    while(t--)
    {   memset(in,0,sizeof(in));
        memset(in,0,sizeof(in));
        n=read();
        for(i=1;i<=n;i++)
        {
           a[i]=read();
        }
        if(n==1)
        {
            printf("%d\n",a[1]);
            continue;
        }
        int minum1=min(a[1],a[2]);
        int minum2=max(a[1],a[2]);
        int index1=0;
        int index2=0;
        if(a[1]>a[2])
        {
            index1=2;
            index2=1;
        }
        else
        {
            index1=1;
            index2=2;
        }
        for(i=2;i<=n;i++)
        {
            if(a[i]<minum1)
            {   minum2=minum1;
            index2=index1;
                index1=i;
                minum1=a[i];
            }
            else if(a[i]==minum1&&index1!=2)
            {
                minum2=minum1;
                index2=index1;
            }
            else if(a[i]>minum1&&a[i]<=minum2)
            {
                in[index1]++;
                minum2=a[i];
                index2=i;
                in[i]++;
            }
            else if(a[i]>minum1)
            {
                in[i]++;
            }
        }
//        for(i=1;i<=n;i++)
//        {
//            printf("%d ",in[i]);
//        }
       int minn=1e9;
       int minnn=1e9;
       for(i=1;i<=n;i++)
       {
           if(in[i]<minn||(in[i]==minn&&a[i]<minnn))
           {
               minnn=a[i];
               minn=in[i];
           }
       }
       printf("%d\n",minnn);
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/zyf3855923/p/9039914.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值