Kattis Cupid 莫队算法

There are  K K different languages in the world. Each person speaks one and only one language. There are exactly  N N single men and  N N single women.

Cupid, the god of love, wants to match every single man to a single woman, and vice versa. Everybody wants to find a partner who speaks the same language as s/he does. Communication between the couple is very important! Cupid asks these  N N men to stand in a line, and likewise for the  N N women. Cupid knows that the  i ith man speaks language  ai ai and the  i ith woman speaks language  bi bi.

It is too hard for Cupid to match all people at the same time. What Cupid does is to repeatedly look at some specific interval in these two lines, pick the men and women in that interval and find the maximum number of man-woman pairs who speak the same language and can be matched.

Input

  • The first line contains three integers  N N M M and  K K  (1N50000,1M50000,1K1000000) (1≤N≤50000,1≤M≤50000,1≤K≤1000000).

  • The second line contains  N N integers  a0,a1,a2,,aN1 a0,a1,a2,…,aN−1, where  ai ai ( 0ai<K 0≤ai<K) is the language spoken by the  i ith man.

  • The third line contains  N N integers  b0,b1,b2,,bN1 b0,b1,b2,…,bN−1, where  bi bi ( 0bi<K 0≤bi<K) is the language spoken by the  i ith woman.

  • In the next  M M lines, each line contains two integers  L L and  R R ( 0LR<N 0≤L≤R<N), representing the starting and ending index of the interval. That is, Cupid is looking at men  L,L+1,,R L,L+1,…,R and women  L,L+1,,R L,L+1,…,R.

Output

For each interval, print the maximum number of couples Cupid could match.

Sample Input 1Sample Output 1
3 4 2
0 0 1
0 0 0
0 0
2 2
0 1
1 2
1
0
2
1

题意:

有K种不同的语言,每个人只说一种语言。有N个男性,N个女性。
现在需要进行男女配对,每个人都想找一个和自己用同一种语言的异性,第i个男性的语言是ai, 第i个女性的语言是bi。
现在给出M个区间作为询问,每次需要回答在[L, R]区间的男女最多能够配对成多少对情侣。


题解:裸的莫队算法。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define rep(i,a,b) for(i=a;i<=b;i++)
#define per(i,a,b) for(i=a;i>=b;i--)
int n,m,k;
struct node{
	int l,r,lab;
}e[50005];
bool cmp(node a,node b){  
    if((a.l)/250==(b.l)/250){  
        return a.r<b.r;  
    }  
    else return a.l<b.l;  
}  
int a[50005],b[50005],ans[50005],num1[1000005],num2[1000005];
int main(){
	scanf("%d%d%d",&n,&m,&k);
	int i,j,x,y;
	rep(i,1,n)scanf("%d",&a[i]);
	rep(i,1,n)scanf("%d",&b[i]);
	rep(i,1,m)scanf("%d%d",&e[i].l,&e[i].r),e[i].lab=i,e[i].l+=1,e[i].r+=1;
	sort(e+1,e+1+n,cmp);
	int now=(a[1]==b[1]),ls=1,rs=1;
	num1[a[ls]]++;
	num2[b[ls]]++;
	rep(i,1,m){
		while(ls<e[i].l){
			num1[a[ls]]--;
			num2[b[ls]]--;
			if(a[ls]==b[ls]){
				now--;
				ls++;
				continue;
			}
			if(num2[a[ls]]>num1[a[ls]])now--;
			if(num1[b[ls]]>num2[b[ls]])now--;
			ls++;
		}
		while(ls>e[i].l){
			ls--;
			num1[a[ls]]++;
			num2[b[ls]]++;
			if(a[ls]==b[ls]){
				now++;
				continue;
			}
			if(num2[a[ls]]>=num1[a[ls]])now++;
			if(num1[b[ls]]>=num2[b[ls]])now++;
		}
		while(rs<e[i].r){
			rs++;
			num1[a[rs]]++;
			num2[b[rs]]++;
			if(a[rs]==b[rs]){
				now++;
				continue;
			}
			if(num2[a[rs]]>=num1[a[rs]])now++;
			if(num1[b[rs]]>=num2[b[rs]])now++;
		}
		while(rs>e[i].r){
			num1[a[rs]]--;
			num2[b[rs]]--;
			if(a[rs]==b[rs]){
				rs--;
				now--;
				continue;
			}
			if(num2[a[rs]]>num1[a[rs]])now--;
			if(num1[b[rs]]>num2[b[rs]])now--;
			rs--;
		}
		ans[e[i].lab]=now;
	}
	rep(i,1,m)printf("%d\n",ans[i]);
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"BIOS Cupid" 是指机器人企业BIOS携手Cupid Robot推出的一款婚介机器人服务。Cupid意为爱神,BIOS Cupid即以其冷静、理性和高效的方式,辅助人们在婚恋领域中寻找合适的伴侣。 BIOS Cupid通过分析用户的个人资料、喜好和兴趣爱好等信息,运用机器学习算法进行大数据分析,提供个性化的婚恋匹配服务。该服务可以帮助用户快速、准确地找到与其需求和期望相匹配的伴侣。 BIOS Cupid在婚恋匹配中具有独特的优势。一方面,机器人通过分析大量数据来进行匹配,相比传统人工匹配更加客观和准确。另一方面,机器人不会受到人类情感和主观因素的影响,保持了冷静、客观的态度,从而帮助用户避免了主观偏见和情感决策带来的风险。 此外,BIOS Cupid还可提供一系列婚姻咨询服务,如情感建议、恋爱技巧等。通过机器学习和人工智能的技术,BIOS Cupid能够不断学习和更新,为用户提供更加精准、全面的婚恋匹配和咨询服务。 然而,值得关注的是,尽管BIOS Cupid拥有许多优势,但机器人不能完全代替人类在婚恋领域的自主选择和判断。最终的决定还应该由用户自己根据个人情感和价值观作出。 总之,BIOS Cupid作为一款婚介机器人服务,致力于通过机器学习和大数据分析,为用户提供个性化、高效的婚恋匹配和咨询服务,辅助人们在婚姻领域找到合适的伴侣。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值