Codeforces Round #450 (Div. 2) C. Remove Extra One

题目链接
题意:让你去掉一个数,使得剩下的数的record最多,当 1 j < i a j < a i 1 \leq j< i的a_j<a_i a i a_i r e c o r d record 数。
解法:遍历数组每个数,记录最大和次大值,用 c n t cnt 数组记录,移除某个数后造成的 r e c o r d record 数变化量。然后输出最优的即可。

#include<bits/stdc++.h>

#define LL long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back

using namespace std;

LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
int n,a[100003],pos[100003],cnt[100003];
int main(){
	ios::sync_with_stdio(false);
	cin>>n;
	int ans=1,m1=-32323,m2=-32323;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		if(a[i]>m1){
			m2=m1;
			m1=a[i];
			cnt[a[i]]=-1;
		}else if(a[i]>m2)m2=a[i],cnt[m1]++;
	}
	int ret=-32323;
	for(int i=1;i<=n;i++){
		if(cnt[i]>ret){
			ret=cnt[i];
			ans=i;
		}
	}
	cout<<ans<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40655981/article/details/88404151