链接:
https://www.nowcoder.com/acm/contest/123/E
来源:牛客网
来源:牛客网
时间限制:C/C++ 5秒,其他语言10秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
We define an element
in a sequence "good", if and only if there exists
(1≤j<i) such that
.
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,. For each test case, the first line contains an integer , representing the length of the given permutation. The second line contains n integersp1,p2,…,pn , representing the given permutation p. It's guaranteed that .
输出描述:
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.
示例1
输入
2 1 1 5 5 1 2 3 4
输出
1 5
#define happy
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define all(a) (a).begin(),(a).end()
#define pll pair<ll,ll>
#define vi vector<int>
#define P pair<int,int>
#define pb push_back
const int inf=0x3f3f3f3f;
ll rd(){
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int N=1e6+10;
ll ft[N],sd[N],a[N];
int cnt[N];
bool flag[N];
int main(){
#ifdef happy
freopen("in.txt","r",stdin);
#endif
int t=rd();
while(t--){
ll now=inf,pre=inf;
ll n=rd();
rep(i,1,n){
a[i]=rd();
ft[i]=now,sd[i]=pre;
if(a[i]<=now){pre=now;now=a[i];}
else if(a[i]<pre)pre=a[i];
}
rep(i,1,n){
if(a[i]<=ft[i])flag[i]=1;
else flag[i]=0;
}
ll k=0;
rep(i,1,n){
if(flag[i]==1){
k=i;
cnt[k]=0;
}else if(a[i]<=sd[i])
cnt[k]++;
}
ll ans=inf;
rep(i,1,n)if(flag[i]==1&&cnt[i]==0)ans=min(ans,a[i]);
if(ans==inf){
rep(i,1,n){
if(flag[i]==0)ans=min(ans,a[i]);
else if(cnt[i]==1)ans=min(ans,a[i]);
}
}
printf("%lld\n",ans);
}
}
本文解析了一道ACM竞赛中的序列优化问题,通过定义“好”元素并给出算法实现,旨在找到移除哪个元素能使序列中“好”元素数量最大化。提供了完整的C++代码实现。

586

被折叠的 条评论
为什么被折叠?



