Sequence(思维题)

本文介绍了一种算法问题,即如何通过从排列中移除一个元素来最大化所谓的“好”元素的数量。“好”元素是指在序列中存在比它小的前驱元素。文章详细讨论了解决方案,并提供了一个具体的实现示例。

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

Problem Description
We define an element a_iai? in a sequence “good”, if and only if there exists a j(1\le j < i)j(1≤j小于i) such that a_j < a_iaj?小于ai?.
Given a permutation pp of integers from 11 to nn. Remove an element from the permutation such that the number of “good” elements is maximized.

Input
The input consists of several test cases. The first line of the input gives the number of test cases, T(1\le T\le 10^3)T(1≤T≤103).
For each test case, the first line contains an integer n(1\le n\le 10^6)n(1≤n≤106), representing the length of the given permutation.
The second line contains nn integers p_1,p_2,\cdots,p_n(1\le p_i\le n)p1?,p2?,?,pn?(1≤pi?≤n), representing the given permutation pp.
It’s guaranteed that \sum n\le 2\times 10^7∑n≤2×107.

Output
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.

Sample Input
2
1
1
5
5 1 2 3 4
Sample Output
1
5
Hint
由于本 OJ 评测机不如现场赛评测机优秀,故时限放宽。

Source
“浪潮杯”山东省第九届ACM大学生程序设计竞赛(感谢山东财经大学)

感觉自己思维题就是弱,赛场上没做出来,本来应该稳下来思考的。
如果第i个是不好数,那么不好数是【0,i】区间里最小的数;
对删除后的后果进行分析一下也能出来:
如果删除好数,最好也就使good数量减一,一定能找到一个
如果删除不好数,那一定要删除完全依赖他的good数为0的不好数,或者删除完全依赖他的good数为一的不好数
在这些情况里找出最小的就行

#include<bits/stdc++.h>
using namespace std;
const int N = 1000005;
typedef long long LL;
const int inf = 0x3f3f3f3f;

//不好的数肯定是他前面所有值中最小值
bool isgood[N];
int a[N];
int first[N];
int second[N];
int cnt[N];

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(isgood,true,sizeof(isgood));
        int n;
        scanf("%d",&n);
        int MIN = inf;
        int now = inf,pre = inf;
        for(int i = 0;i < n;++i)
        {
            scanf("%d",&a[i]);
            first[i] = now; second[i] = pre;
            if(a[i] <= now){
                pre = now;
                now = a[i];
            }
            else{
                if(a[i] < pre)
                    pre = a[i];
            }
            if(MIN > a[i]){
                isgood[i] = false;
                MIN = a[i];
            }
        }
//        for(int i = 0;i < n;++i)
//            cout << isgood[i] << endl;
        //cnt存储的是去掉那个不好数,一定影响的好数的个数
//        for(int i = 0;i < n;++i)
//            cout << first[i] << " " << second[i] << endl;
        int k = 0;
        for(int i = 0;i < n;++i)
        {
            if(!isgood[i]){
                k = i;
                cnt[k] = 0;
            }
            else{
                if(a[i] <= second[i])
                    cnt[k]++;
            }
        }
//        for(int i = 0;i < n;++i)
//            cout << cnt[i] << endl;
        int tmp = inf;
        for(int i = 0;i < n;++i)
        {
            if(!isgood[i] && cnt[i] == 0){
                tmp = min(tmp,a[i]);
            }
        }
        if(tmp == inf){
            for(int i = 0;i < n;++i)
            {
                if(isgood[i]){
                    tmp = min(tmp,a[i]);
                }
                else{
                    if(cnt[i] == 1)
                        tmp = min(tmp,a[i]);
                }
            }
        }
        printf("%d\n",tmp);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值