Sequence II

6990: Sequence II

时间限制: 3 Sec  内存限制: 128 MB
提交: 206  解决: 23
[提交][状态][讨论版][命题人:admin]

题目描述

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
骚的输入操作,用scanf()居然超时!

#include <bits/stdc++.h>
using namespace std;
int scan()
{
int res=0;
char ch;
ch=getchar();
if(ch>='0' && ch<='9')
{
res=ch-'0';
}
while((ch=getchar())>='0' && ch<='9')
{
res=res*10+ch-'0';
}
return res;
}
void out(int a)
{
if(a>9)
{
out(a/10);
}
putchar(a%10+'0');
}
int b[1000100];
int a[1000100];
int main()
{
int t,n,rr,zxz,minn,minnn;
t=scan();
while(t--)
{
memset(b,0,sizeof(b));
n=scan();
if(n==1)
{
a[0]=scan();
out(a[0]);
putchar(' ');
}
if(n==2)
{
a[0]=scan();
a[1]=scan();
out(min(a[0],a[1]));
putchar(' ');
}
if(n>=3)
{
for(int i=0; i<=n-1; i++)
{
a[i]=scan();
if(i==1)
{
minnn=min(a[0],a[1]);
minn=max(a[0],a[1]);
if(a[0]<a[1])
{
b[a[0]]++;
b[a[1]]++;
}
}
else if(i>1)
{
if(a[i]<minnn)
{
minn=minnn;
minnn=a[i];
}
else if(a[i]>minnn && a[i]<minn)
{
b[minnn]++;
b[a[i]]++;
minn=a[i];
}
else if(a[i]>minn)
{
b[a[i]]++;
}
}
}
rr=1e9+7;
zxz=1e9+7;
for(int i=0;i<=n-1;i++)
{
if(b[a[i]]<zxz)
{
zxz=b[a[i]];
rr=a[i];
}
else if(b[a[i]]==zxz && a[i]<rr)
{
rr=a[i];
}
}
out(rr);
putchar(' ');
}
}
return 0;
}

原文地址:https://www.cnblogs.com/lglh/p/9079116.html