题目描述
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.
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, 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 ",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 ",minnn); } return 0; }