2018.09.29【Atcoder Regular Contest 103】C - /\/\/\/(贪心)

传送门


解析:

这道题是个显然的贪心,考虑在交错序列中选择两个不在同一列上的数,那么除此以外的所有数都要被替换,为了替换尽可能少,就需要贪心选择最大的。

然而最大的可能在在两个序列中是同一个数,题目要求不能将两列替换成同样的数。所以我们同时维护次大值。注意这里的次大值维护需要多一点考虑,具体在代码里面体现的很清楚了。


代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
#define gc getchar
#define pc putchar
#define cs const

inline
ll getint(){
    re ll num;
    re char c;
    re bool f=0;
    while(!isdigit(c=gc()))f^=c=='-';num=c^48;
    while(isdigit(c=gc()))num=(num<<1)+(num<<3)+(c^48);
    return f?-num:num;
}

inline
void outint(ll a){
	static char ch[23];
	if(a==0)pc('0');
	while(a)ch[++ch[0]]=a-a/10*10,a/10;
	while(ch[0])pc(ch[ch[0]--]^48);
}

int n;
cs int N=100005;
int cnt[2][N];
int maxn[2],num[2],submaxn[2],subnum[2];
signed main(){
	n=getint();
	for(int re i=2;i<=n+1;++i){
		int a=getint();
		++cnt[i&1][a];
		if(maxn[i&1]<cnt[i&1][a]){
			if(num[i&1]==a){
				maxn[i&1]=cnt[i&1][a];
			}
			else {
				subnum[i&1]=num[i&1];
				submaxn[i&1]=maxn[i&1];
				maxn[i&1]=cnt[i&1][a];
				num[i&1]=a;
			}
		}
		else if((cnt[i&1][a]==maxn[i&1]&&num[i&1]!=a)||cnt[i&1][a]>submaxn[i&1]){
			submaxn[i&1]=cnt[i&1][a];
			subnum[i&1]=a;
		}
	}
	int ans;
	if(num[0]!=num[1])ans=n-maxn[0]-maxn[1];
	else ans=n-max(maxn[0]+submaxn[1],maxn[1]+submaxn[0]);
	cout<<ans;
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值