HDU - 2018杭电ACM集训队单人排位赛 - 3 - Problem E. Sequence

简介: HDU - 2018杭电ACM集训队单人排位赛 - 3 - Problem E. Sequence

Problem E.Sequence

Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 220    Accepted Submission(s): 53


Problem Description


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.


Input


The input consists of several test cases. The first line of the input gives the number of test cases, T(1≤T≤103).

For each test case, the first line contains an integer n(1≤n≤106), representing the length of the given permutation.

The second line contains n integers p1,p2,⋯,pn(1≤pi≤n), representing the given permutation p.

It’s guaranteed that ∑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


解题思路:首先要明白,一个好数 a 即在它自己前面(不包括它自己)中若存在一个比它小的数 b 即该数 a 为好数;一个不好数,它总是它之前(包括它自己)出现的所有数中最小的。

考虑删除一个好数还是不好数:


删除一个好数:则总的好数数量将减少一个(因为删除它后能影响到的好数仅有它自己)。

删除一个不好数:考虑删除这个不好数后能影响到的好数有几个,那么总的好数数量就减少几个。

维护:通过 一个最小数 和 一个次小数 的维护即可得到每个数据元素的贡献量,最后找到那个贡献最小的数据元素即可。

AC 代码


/

#include<bits/stdc++.h>
#include<cmath>
#define mem(a,b) memset(a,b,sizeof a);
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
int a[maxn], cnt[maxn];
int main()
{
    int T; scanf("%d",&T);
    int n;
    while(T-- && ~scanf("%d",&n))
    {
        mem(cnt,0);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        int l,r,pre; l=r=INT_MAX;
        for(int i=1;i<=n;i++)
        {
            if(r<a[i]) cnt[i]++;
            else if(l<a[i]) r=a[i], cnt[i]++, cnt[pre]++;
            else r=l, l=a[i], pre=i;
        }
        pre=1;
        for(int i=2;i<=n;i++)
            if(cnt[i]==cnt[pre]&&a[pre]>a[i] || cnt[pre]>cnt[i])
                pre=i;
        printf("%d\n",a[pre]);
    }
    return 0;
}
目录
相关文章
2022天梯赛三月冲刺——PAT (Advanced Level)1013 Battle Over Cities (并查集找连通块)
2022天梯赛三月冲刺——PAT (Advanced Level)1013 Battle Over Cities (并查集找连通块)
116 0
2021年度训练联盟热身训练赛第一场——Early Orders(单调栈)
2021年度训练联盟热身训练赛第一场——Early Orders(单调栈)
63 0
UPC组队赛第三场——G: The Famous ICPC Team Again (主席树)
UPC组队赛第三场——G: The Famous ICPC Team Again (主席树)
104 0
[SDUT 2414] | 山东省第三届省赛 An interesting game | 最小费用最大流
题目描述 Xiao Ming recently designs a little game, in front of player there are N small hillsides put in order, now Xiao Ming wants to increase some hillsides to block the player, so he prepared another M hillsides, but he does not hope it will be too difficult,
168 0
[SDUT 2414] | 山东省第三届省赛 An interesting game | 最小费用最大流
|
存储 测试技术
|
人工智能 Java
HDU - 2018杭电ACM集训队单人排位赛 - 4 - Problem C. Sequence
HDU - 2018杭电ACM集训队单人排位赛 - 4 - Problem C. Sequence
114 0
|
Java
HDU - 2018杭电ACM集训队单人排位赛 - 3 - Problem F. Four-tuples
HDU - 2018杭电ACM集训队单人排位赛 - 3 - Problem F. Four-tuples
144 0
|
人工智能 Java
HDU - 2018杭电ACM集训队单人排位赛 - 4 - Problem J. number sequence
HDU - 2018杭电ACM集训队单人排位赛 - 4 - Problem J. number sequence
140 0
|
机器学习/深度学习 算法 Java
HDU - 2018杭电ACM集训队单人排位赛 - 3 - Problem B. Bullet
HDU - 2018杭电ACM集训队单人排位赛 - 3 - Problem B. Bullet
173 0
|
Java Go
HDU - 2018杭电ACM集训队单人排位赛 - 2 - Problem E. Travel
HDU - 2018杭电ACM集训队单人排位赛 - 2 - Problem E. Travel
130 0

热门文章

最新文章