2021杭电第五场补题

这篇博客介绍了如何运用Boyer-Moore投票算法来解决寻找数组中众数子区间的数目问题。通过遍历数组,动态维护众数候选值和计数,同时使用差分数组和前缀和技巧优化计算,实现O(n)的时间复杂度和O(1)的空间复杂度解决方案。博客提供了两种不同的C++代码实现,详细解释了算法的每一步操作,适合对算法和数据结构感兴趣的读者学习。
摘要由CSDN通过智能技术生成

I Array

题目连接

题目简意:

一个n个数的整数数组,子区间长度大于等于一,多少个子区间有众数。

洛谷原题

Boyer-Moore 投票算法:

时间复杂度O(n),空间复杂度O(1).

基础理解:

历数组nums 中的所有元素,遍历到元素 x 时,进行如下操作:

        如果count=0,则将 x 的值赋给candidate,否则不更新candidate 的值;

        如果 x=candidate,则将 count 加 1,否则将count 减 1。

遍历结束之后,candidate的值可能为主要元素。(一般适用于一定存在众数,candidate的值就是主要元素了)

回到Array:

参考

参考代码:

链接

#include <cstdio>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
const int N = 1000000;
vector<int> place[N + 5];
int main()
{
    int t = 1, n;
    scanf("%d", &t);
    while(t--)
    {
        int x;
        scanf("%d", &n);
        for (int i = 1; i <= n;i++)
        {
            scanf("%d", &x);
            place[x].push_back(i);
        }
        long long ans = 0;
        for (int num = 0; num <= N; num++)
        {
            if(!place[num].size())
                continue;
            place[num].push_back(n + 1);
            long long cnt = 0, tot = 0;
            //cnt表示了当前sum出现的次数,为次数差分数组dif的前缀和
            map<int, long long> dif;
            //第一个众数数字前的情况,共计 1-place[num][0] 个
            int minimum = 1 - place[num][0], now = 1 - place[num][0];
            dif[1]--;
            dif[now]++;
            //now~0 的数全部出现一次
            for (int i = 0; i + 1 < place[num].size(); i++)
            {
                cnt += dif[now];//加上当前一位的值即可
                tot += cnt;
                ans += tot;
                now++;
                dif[now]++;//当前的sum次数增加1
                dif[now + 1]--;
                int delta = place[num][i + 1] - place[num][i] - 1;
                for (; delta > 0 && now > minimum;delta--)
                {
                    tot -= cnt;//先把当前的sum对应次数挖掉
                    ans += tot;
                    cnt -= dif[now - 1];//连带修改
                    now--;
                    dif[now]++;
                    dif[now + 1]--;
                }
                if(delta>0)//跌穿,直接重来
                {
                    dif[minimum]--;
                    minimum -= delta;
                    dif[minimum]++;
                    now = minimum;
                    cnt = 0;
                    tot = 0;
                }
            }
        }
        printf("%lld\n", ans);
        for (int i = 0; i <= N; i++)
            place[i].clear();
    }
    return 0;
}

参考代码 链接


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e6+10;
vector<int>v[maxn];//记录每个众数出现的位置
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int T;  cin>>T;
	while(T--){
		int n;  cin>>n;
		vector<int>a(n+1);   
		unordered_set<int>s; //记录有哪些众数
		for(int i=1; i <= n; i++){
			cin>>a[i];  v[a[i]].push_back(i);  s.insert(a[i]);
		}
		LL ans = 0;
		for(int x : s){//枚举每个数作为众数
			LL res = 0, sum = 0;//x的答案贡献res,当前前缀和sum
			unordered_map<int,int>f1,f2; //前缀和为sum的点有f1[sum]个, f2为f1差分数组
			v[x].push_back(n+1); //防止溢出
			LL k = 0, minn = 0;  //枚举众数的每个位置k,历史前缀最小值minn
			for(int j = 1; j <= n; j++){//枚举每一段-1的起始位置
				if(j > v[x][k])k++;
				if(a[j]!=x && sum==minn){//区间[j,v[x][k]-1]都是-1
					LL len = v[x][k]-1-j;
					f2[sum-len]++; f2[sum+1]--;//区间f1[sum−x,sum-1]+=1
					j += len;
					sum -= len+1;
				}else if(a[j]==x){ //更新答案
					f1[sum]+=f2[sum];//对差分数组求前缀和并更新到f1中去
					f2[sum+1]+=f2[sum];
					f2[sum]=0;
					f1[sum]++;//当前前缀和的个数++
					res += f1[sum];//累加为∑[0,sum−1]f(i​)​
					sum++;
					ans += res;
				}else{
					f1[sum]++; sum--;
					res -= f1[sum];
					ans += res;
				}
				minn = min(minn, sum);
			}
		}
		cout<<ans<<"\n";
		for(auto &i: s)v[i].clear();
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值