• kattis Curious Cupid (莫队算法)


    Curious Cupid

    There are K

    different languages in the world. Each person speaks one and only one language. There are exactly N single men and 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

    men to stand in a line, and likewise for the N women. Cupid knows that the ith man speaks language ai and the ith woman speaks language 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

    , M and K (1N50000,1M50000,1K1000000)

    • .

    • The second line contains N

    integers a0,a1,a2,,aN1, where ai (0ai<K) is the language spoken by the i
    • th man.

    • The third line contains N

    integers b0,b1,b2,,bN1, where bi (0bi<K) is the language spoken by the i
    • th woman.

    • In the next M

    lines, each line contains two integers L and R (0LR<N), representing the starting and ending index of the interval. That is, Cupid is looking at men L,L+1,,R and women 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
    
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <string>
    #include <map>
    #include <stack>
    #include <queue>
    #include <vector>
    #define inf 0x3f3f3f3f
    #define met(a,b) memset(a,b,sizeof a)
    #define pb push_back
    typedef long long ll;
    using namespace std;
    const int N = 5e4+10;
    const int M = 1e6+10;
    ll num[N],up[N],dw[N],ans,aa,bb,cc;
    int n,m,k;
    int x[N],y[N],pos[N];
    int cntx[M],cnty[M];
    struct qnode {
        int l,r,id;
    } qu[N];
    bool cmp(qnode a,qnode b) {
        if(pos[a.l]==pos[b.l])
            return a.r<b.r;
        return pos[a.l]<pos[b.l];
    }
    void update(int p,int d) {
        if(x[p]==y[p]) {
            cntx[x[p]]+=d;
            cnty[x[p]]+=d;
            ans+=d;
        } else {
            if(d==1) {
                if(cnty[x[p]]>cntx[x[p]]) {
                    ans+=d;
                }
                if(cntx[y[p]]>cnty[y[p]]) {
                    ans+=d;
                }
            } else {
                if(cnty[x[p]]>=cntx[x[p]]) {
                    ans+=d;
                }
                if(cntx[y[p]]>=cnty[y[p]]) {
                    ans+=d;
                }
            }
            cntx[x[p]]+=d;
            cnty[y[p]]+=d;
        }
    }
    int main() {
        int bk,pl,pr,id;
        scanf("%d%d%d",&n,&m,&k);
        memset(num,0,sizeof num);
        bk=ceil(sqrt(1.0*n));
        for(int i=1; i<=n; i++) {
            scanf("%d",&x[i]);
            pos[i]=(i-1)/bk;
        }
        for(int i=1; i<=n; i++) {
            scanf("%d",&y[i]);
        }
        for(int i=0; i<m; i++) {
            scanf("%d%d",&qu[i].l,&qu[i].r);
            qu[i].l++;
            qu[i].r++;
            qu[i].id=i;
        }
        sort(qu,qu+m,cmp);
        pl=1,pr=0;
        ans=0;
        for(int i=0; i<m; i++) {
            id=qu[i].id;
            if(pr<qu[i].r) {
                for(int j=pr+1; j<=qu[i].r; j++)
                    update(j,1);
            } else {
                for(int j=pr; j>qu[i].r; j--)
                    update(j,-1);
            }
            pr=qu[i].r;
            if(pl<qu[i].l) {
                for(int j=pl; j<qu[i].l; j++)
                    update(j,-1);
            } else {
                for(int j=pl-1; j>=qu[i].l; j--)
                    update(j,1);
            }
            pl=qu[i].l;
            up[id]=ans;
        }
    
        for(int i=0; i<m; i++)
            printf("%d
    ",up[i]);
        return 0;
    }
  • 相关阅读:
    【原创】大叔问题定位分享(21)spark执行insert overwrite非常慢,比hive还要慢
    【原创】大叔经验分享(14)spark on yarn提交任务到集群后spark-submit进程一直等待
    【原创】大叔问题定位分享(20)hdfs文件create写入正常,append写入报错
    【原创】大叔问题定位分享(19)spark task在executors上分布不均
    【原创】大数据基础之Spark(4)RDD原理及代码解析
    【原创】大叔问题定位分享(18)beeline连接spark thrift有时会卡住
    【原创】大叔问题定位分享(17)spark查orc格式数据偶尔报错NullPointerException
    【原创】大叔经验分享(13)spark运行报错WARN Utils: Service 'sparkDriver' could not bind on port 0. Attempting port 1.
    linux定时任务
    source导入错码解决办法
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6476752.html
Copyright © 2020-2023  润新知