Give life a little publicity.🍺|

Mr_Azz

园龄:2年1个月粉丝:0关注:2

📂题解
🔖题解
2024-05-31 21:59阅读: 7评论: 0推荐: 0

UVA11235 Frequent values

Frequent values

双倍经验:

UVA11235

SP1684

求区间众数的出现次数。

Analysis 分析

莫队。

Solution 莫队

考虑扩展和删除一个数。

add:

扩展一格是简单的,只需要记一个数组 cntx 代表 x 出现的次数,用来更新当前答案 res 即可。

del:

删除一格我们发现只记一个数组 cntx 是不够的。我们再引入 totx 代表出现了 x 次的数有几个。

这时 cntx=restotcntx=0 ,如果两个条件都满足,就表示众数的个数少了一个,即 res

观察到 ai 值域包含负数,可以把所有的 ai 都加上 1e5。

同时注意莫队移动的顺序。

Code 代码

int n,T,blo,res=-1;
int a[N],ans[N];
int cnt[N*2],tot[N*2];//cnt[x]: x出现的次数 tot[x]: 出现次数为x的数的个数
inline int num(int x){return x/blo;}
struct ask{
	int l,r,id;
	bool operator<(const ask &A)const{
		if(num(l)!=num(A.l)) return num(l)<num(A.l);
		return (num(A.l)&1)?r<A.r:r>A.r;
	}
}q[N];
inline void add(int x){
	x=a[x];
	tot[cnt[x]]--;cnt[x]++;tot[cnt[x]]++;
	res=max(res,cnt[x]);
	return ;
}
inline void del(int x){
	x=a[x];
	tot[cnt[x]]--;
	if(cnt[x]==res&&tot[cnt[x]]==0) res--;
	cnt[x]--;tot[cnt[x]]++;
	return ;
}
int main(){
	while(1){
		res=-1;
		read(n);
		if(!n) return 0;//多测退出
		read(T);
        mem(tot,0);mem(cnt,0);//多测清空
		blo=sqrt(n);
		for(rint i=1;i<=n;i++) read(a[i]),a[i]+=N;
		for(rint i=1;i<=T;i++){
			int x,y;read(x,y);
			q[i]={x,y,i};
		}
		sort(q+1,q+T+1);
		int l=1,r=0;	
		for(rint i=1;i<=T;i++){
            //注意转移顺序,会T
	        while(l<q[i].l) del(l++);
	        while(l>q[i].l) add(--l);
	        while(r>q[i].r) del(r--);
	        while(r<q[i].r) add(++r);
	        ans[q[i].id]=res;
	    }
	    for(rint i=1;i<=T;i++) printf("%lld\n",ans[i]); 
	}
    return 0;
}

本文作者:Mr-Az

本文链接:https://www.cnblogs.com/Mr-Az/p/18225341/UVA-11235

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Mr_Azz  阅读(7)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 使一颗心免于哀伤 知更鸟
使一颗心免于哀伤 - 知更鸟
00:00 / 00:00
An audio error has occurred.

作词 : 黑金雨

作曲 : 王可鑫 Eli.W (HOYO-MiX)

编曲 Arranger : 王可鑫 Eli.W (HOYO-MiX)

制作人 Producer : 王可鑫 Eli.W (HOYO-MiX)

Birds are born with no shackles

Then what fetters my fate?

Blown away, the white petals

Leave me trapped in the cage.

The endless isolation

Can't wear down my illusion

Someday, I’ll make a dream unchained

Let my heart bravely spread the wings

Soaring past the night

To trace the bright moonlight

Let the clouds heal me of the stings

Gently wipe the sorrow off my life

I dream

What is meant by “miracle”,

What is meant by “miracle”,

A word outside my days?

Once again, repeat warbles

But how could I escape?

No further hesitation

On those unanswered questions

So now, I’ll make a dream unchained

Let my heart bravely spread the wings

Soaring past the night

To trace the bright moonlight

Let the clouds heal me of the stings

Gently wipe the sorrow off my life

I dream

Let my heart bravely spread the wings

Soaring past the night

To trace the bright moonlight

Let the clouds heal me of the stings

Gently wipe the sorrow off my life

I dream

I dream

人声 Vocal Artist:Chevy

人声 Vocal Artist:Chevy

木吉他 Acoustic Guitar:赵新博

电吉他 Electric Guitar:赵新博

架子鼓 Drums:眭逸凡Patrick

乐队 Orchestra:国际首席爱乐乐团 International Master Philharmonic Orchestra

人声录音棚 Vocal Recording Studio:The Hideout Recording Studio

人声录音师 Vocal Recording Engineer:Luis Pacheco

乐器录音棚 Instrumental Recording Studio:升赫录音棚Soundhub Studio/九紫天诚录音棚 SKY FIRE STUDIO

乐器录音师 Instrumental Recording Engineer:Kevin刘瀚文@Soundhub Studios/董方昱

混音师 Mixing Engineer:宫奇Gon (HOYO-MiX)

母带制作 Mastering Engineer:宫奇Gon (HOYO-MiX)

出品 Produced by:HOYO-MiX