Sequence(思维题)

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;
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页