zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    一点点深夜感慨(还有居家学习日常)
    区间dp 矩阵连乘
    logstash grok match 对等于号,和中括号做筛选
    grok内置的默认类型有很多种,查看所有默认类型
    python开发技术要求
    django--models操作--备注
    django程序--settings.py文件备注
    django-admin 命令备注
    官网提供jumpserver安装文档--复盘 CentOS 7 安装文档
    安装jumpserver,提示错误
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6476752.html
Copyright © 2011-2022 走看看