zoukankan      html  css  js  c++  java
  • bzoj2038 [2009国家集训队]小Z的袜子(hose)

    Description

    作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿。终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……
    具体来说,小Z把这N只袜子从1到N编号,然后从编号L到R(L 尽管小Z并不在意两只袜子是不是完整的一双,甚至不在意两只袜子是否一左一右,他却很在意袜子的颜色,毕竟穿两只不同色的袜子会很尴尬。
    你的任务便是告诉小Z,他有多大的概率抽到两只颜色相同的袜子。当然,小Z希望这个概率尽量高,所以他可能会询问多个(L,R)以方便自己选择。

    Input

    输入文件第一行包含两个正整数N和M。N为袜子的数量,M为小Z所提的询问的数量。接下来一行包含N个正整数Ci,其中Ci表示第i只袜子的颜色,相同的颜色用相同的数字表示。再接下来M行,每行两个正整数L,R表示一个询问。

    Output

    包含M行,对于每个询问在一行中输出分数A/B表示从该询问的区间[L,R]中随机抽出两只袜子颜色相同的概率。若该概率为0则输出0/1,否则输出的A/B必须为最简分数。(详见样例)

    Sample Input

    6 4
    1 2 3 3 3 2
    2 6
    1 3
    3 5
    1 6

    Sample Output

    2/5
    0/1
    1/1
    4/15
    【样例解释】
    询问1:共C(5,2)=10种可能,其中抽出两个2有1种可能,抽出两个3有3种可能,概率为(1+3)/10=4/10=2/5。
    询问2:共C(3,2)=3种可能,无法抽到颜色相同的袜子,概率为0/3=0/1。
    询问3:共C(3,2)=3种可能,均为抽出两个3,概率为3/3=1/1。
    注:上述C(a, b)表示组合数,组合数C(a, b)等价于在a个不同的物品中选取b个的选取方案数。
    【数据规模和约定】
    30%的数据中 N,M ≤ 5000;
    60%的数据中 N,M ≤ 25000;
    100%的数据中 N,M ≤ 50000,1 ≤ L < R ≤ N,Ci ≤ N。

    正解:莫队算法

    板子题,不多说了。。

    //It is made by wfj_2048~
    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <vector>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #define inf 1<<30
    #define il inline
    #define RG register
    #define ll long long
    #define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
    
    using namespace std;
    
    struct node{ int l,r,bl,id; }q[50010];
    struct point{ ll a,b; }ans[50010];
    
    int c[50010],cnt[50010],bl[50010],n,m,block;
    
    il int gi(){
        RG int x=0,q=0; RG char ch=getchar();
        while ((ch<'0' || ch>'9') && ch!='-') ch=getchar(); if (ch=='-') q=1,ch=getchar();
        while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q ? -x : x;
    }
    
    il int cmp(const node &a,const node &b){ return a.bl<b.bl || (a.bl==b.bl && a.r<b.r); }
    
    il ll gcd(RG ll a,RG ll b){ return b ? gcd(b,a%b) : a; }
    
    il void work(){
        n=gi(),m=gi(),block=sqrt(n); for (RG int i=1;i<=n;++i) c[i]=gi(),bl[i]=(i-1)/block+1;
        for (RG int i=1;i<=m;++i) q[i].l=gi(),q[i].r=gi(),q[i].bl=bl[q[i].l],q[i].id=i;
        sort(q+1,q+m+1,cmp); int L=1,R=0; ll Ans=0;
        for (RG int i=1;i<=m;++i){
    	if (q[i].l==q[i].r){ ans[q[i].id].a=0,ans[q[i].id].b=1; continue; }
    	while (L>q[i].l) L--,Ans+=(ll)cnt[c[L]],cnt[c[L]]++;
    	while (R<q[i].r) R++,Ans+=(ll)cnt[c[R]],cnt[c[R]]++;
    	while (L<q[i].l) cnt[c[L]]--,Ans-=(ll)cnt[c[L]],L++;
    	while (R>q[i].r) cnt[c[R]]--,Ans-=(ll)cnt[c[R]],R--;
    	ans[q[i].id].a=Ans,ans[q[i].id].b=(ll)(q[i].r-q[i].l+1)*(ll)(q[i].r-q[i].l)>>1;
    	RG ll k=gcd(ans[q[i].id].a,ans[q[i].id].b); ans[q[i].id].a/=k,ans[q[i].id].b/=k;
        }
        for (RG int i=1;i<=m;++i) printf("%lld/%lld
    ",ans[i].a,ans[i].b); return;
    }
    
    int main(){
        File("sock");
        work();
        return 0;
    }
    


  • 相关阅读:
    新建立了个集邮 Blog
    删除8848的mysearch
    VS2005的中国发布会
    免费的PDF生成工具
    FreeBASIC
    今天收到了WinZip发来的免费License
    简洁的 Bash 编程技巧
    benhuan039sblog.wordpress.20121111.xml_.txt
    新浪微博除掉推荐微博
    自制力也是一种力量
  • 原文地址:https://www.cnblogs.com/wfj2048/p/6416597.html
Copyright © 2011-2022 走看看