/\/\/\/(摆动数组的应用&结构体数组标记法)
A sequence a1,a2,…,an is said to be /\/\/\/ when the following conditions are satisfied:
译:一个序列a1,a2,....an如果是/\/\/\/数列,那么会满足下列条件
For each i=1,2,…,n−2, ai=ai+2.
Exactly two different numbers appear in the sequence.
You are given a sequence v1,v2,…,vn whose length is even. We would like to make this sequence /\/\/\/ by replacing some of its elements. Find the minimum number of elements that needs to be replaced.
译:对于每个i=1,2,……,n-2,ai=a(i+2),在每个序列中出现两个不相等数字。现有一个序列v1,v2,...,vn,它的长度是一个偶数,我们可以通过换一些数字来把这个序列变成一个/\/\/\/数组。输出所需替换的最小数目。
Constraints
2≤n≤1e5
n is even.
1≤vi≤1e5
vi is an integer.
输入
Input is given from Standard Input in the following format:
n
v1 v2 … vn
输出
Print the minimum number of elements that needs to be replaced.
样例输入
复制样例数据
4
3 1 3 2
样例输出
1
提示
The sequence 3,1,3,2 is not /\/\/\/, but we can make it /\/\/\/ by replacing one of its elements: for example, replace the fourth element to make it 3,1,3,1.
既然这个摆动数组里只能有两个数字,而且必须分别在奇数位和偶数位上,那么我们找出奇数位,偶数位上出现次数最多的数字后再用总数减去他们即可。但是,题目有说:序列里的两个数字必须不等,也就是说如果相等,要拿除了那个数之外第二多的数字来进行计算,那单纯用一个数组标记元素的办法可能就有些疲软,所以我们用结构体去标记数组的元素,恰好,这个题目里最大元素也就是1e5,显然数组标记法是可行的。
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
#include <algorithm>
#include <iomanip>
#include <cstring>
#include <cmath>
//#include <minmax.h>
#define DETERMINATION main
#define lldin(a) scanf("%lld",&a)
#define din(a) scanf("%d",&a)
#define reset(a,b) memset(a,b,sizeof(a))
const int INF=0x3f3f3f3f;
const int LIM=1e5+75;
using namespace std;
typedef long long ll;
/**you can feel the pulse of your destiny...
The nervous feeling fills you with determination.**/
int n;
int a[LIM];
struct nu
{
int value,mx;
}hhhhhh[LIM],qqqqqq[LIM];
bool cmp(nu a,nu b)
{
return a.mx>b.mx;
}
int DETERMINATION()
{
cin>>n;
for(int i=1; i<=n; i++)
cin>>a[i];
for(int i=1; i<=n; i+=2)//找出奇数位上出现次数最多的数字
{
hhhhhh[a[i]].mx++;
hhhhhh[a[i]].value=a[i];
}
for(int i=2; i<=n; i+=2)//找出偶数位上出现次数最多的数字
{
qqqqqq[a[i]].mx++;
qqqqqq[a[i]].value=a[i];
}
ll ans=0;
sort(qqqqqq+1,qqqqqq+LIM+1,cmp);//通过排序找出最大值,由于只有1e5个元素,排序不会造成什么时间上的浪费。
sort(hhhhhh+1,hhhhhh+LIM+1,cmp);
//cout<<qqqqqq[1].value<<" "<<hhhhhh[1].value<<endl;
if(qqqqqq[1].value==hhhhhh[1].value)//如果奇数位和偶数位都是同一个数字出现次数最多。
{
ans=min((n-hhhhhh[2].mx-qqqqqq[1].mx),(n-hhhhhh[1].mx-qqqqqq[2].mx));
//那么枚举两种情况来求最小值。
}
else
ans=n-hhhhhh[1].mx-qqqqqq[1].mx;//不然照常计算即可。
cout<<ans<<endl;
return 0;
}

浙公网安备 33010602011771号