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

    题目链接:

    http://www.lydsy.com/JudgeOnline/problem.php?id=2038

    专题练习:

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=29469#overview

    2038: [2009国家集训队]小Z的袜子(hose)

    Time Limit: 20 Sec  Memory Limit: 259 MB
    Submit: 966  Solved: 472
    [Submit][Status]

    Description

    作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿。终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命…… 具体来说,小Z把这N只袜子从1到N编号,然后从编号L到R(L

    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。

    HINT

     

    Source

    [Submit][Status]


    HOME Back


    莫队算法可以解决一类不修改、离线查询问题。

    构造曼哈顿最小生成树的做法还没有写。

    写了个直接分段解决的办法。

    把1~n分成sqrt(n)段。

    unit = sqrt(n)

    m个查询先按照第几个块排序,再按照 R排序。

    然后直接求解。

      1 /* ***********************************************
      2 Author        :kuangbin
      3 Created Time  :2013/8/16 19:07:51
      4 File Name     :F:2013ACM练习专题学习莫队算法小Z的袜子.cpp
      5 ************************************************ */
      6 
      7 #include <stdio.h>
      8 #include <string.h>
      9 #include <iostream>
     10 #include <algorithm>
     11 #include <vector>
     12 #include <queue>
     13 #include <set>
     14 #include <map>
     15 #include <string>
     16 #include <math.h>
     17 #include <stdlib.h>
     18 #include <time.h>
     19 using namespace std;
     20 
     21 const int MAXN = 50010;
     22 const int MAXM = 50010;
     23 struct Query
     24 {
     25     int L,R,id;
     26 }node[MAXM];
     27 long long gcd(long long a,long long b)
     28 {
     29     if(b == 0)return a;
     30     return gcd(b,a%b);
     31 }
     32 struct Ans
     33 {
     34     long long a,b;//分数a/b
     35     void reduce()//分数化简
     36     {
     37         long long d = gcd(a,b);
     38         a /= d; b /= d;
     39     }
     40 }ans[MAXM];
     41 int a[MAXN];
     42 int num[MAXN];
     43 int n,m,unit;
     44 bool cmp(Query a,Query b)
     45 {
     46     if(a.L/unit != b.L/unit)return a.L/unit < b.L/unit;
     47     else return a.R < b.R;
     48 }
     49 void work()
     50 {
     51     long long temp = 0;
     52     memset(num,0,sizeof(num));
     53     int L = 1;
     54     int R = 0;
     55     for(int i = 0;i < m;i++)
     56     {
     57         while(R < node[i].R)
     58         {
     59             R++;
     60             temp -= (long long)num[a[R]]*num[a[R]];
     61             num[a[R]]++;
     62             temp += (long long)num[a[R]]*num[a[R]];
     63         }
     64         while(R > node[i].R)
     65         {
     66             temp -= (long long)num[a[R]]*num[a[R]];
     67             num[a[R]]--;
     68             temp += (long long)num[a[R]]*num[a[R]];
     69             R--;
     70         }
     71         while(L < node[i].L)
     72         {
     73             temp -= (long long)num[a[L]]*num[a[L]];
     74             num[a[L]]--;
     75             temp += (long long)num[a[L]]*num[a[L]];
     76             L++;
     77         }
     78         while(L > node[i].L)
     79         {
     80             L--;
     81             temp -= (long long)num[a[L]]*num[a[L]];
     82             num[a[L]]++;
     83             temp += (long long)num[a[L]]*num[a[L]];
     84         }
     85         ans[node[i].id].a = temp - (R-L+1);
     86         ans[node[i].id].b = (long long)(R-L+1)*(R-L);
     87         ans[node[i].id].reduce();
     88     }
     89 }
     90 
     91 
     92 int main()
     93 {
     94     //freopen("in.txt","r",stdin);
     95     //freopen("out.txt","w",stdout);
     96     while(scanf("%d%d",&n,&m) == 2)
     97     {
     98         for(int i = 1;i <= n;i++)
     99             scanf("%d",&a[i]);
    100         for(int i = 0;i < m;i++)
    101         {
    102             node[i].id = i;
    103             scanf("%d%d",&node[i].L,&node[i].R);
    104         }
    105         unit = (int)sqrt(n);
    106         sort(node,node+m,cmp);
    107         work();
    108         for(int i = 0;i < m;i++)
    109             printf("%lld/%lld
    ",ans[i].a,ans[i].b);
    110     }
    111     return 0;
    112 }
  • 相关阅读:
    卡尔曼滤波公式
    在博客园主页添加github链接
    博客园插入latex公式
    Leetcode刷题(2020/03/20)
    git设置http代理
    ubuntu下解压.zip文件乱码
    Linux系统中的变量PATH
    【windows】在控制面板卸载软件的时候,出现2502,2503的问题
    替换openjdk的版本时遇到报错Transaction check error
    安装Python3.6.4后,在使用numpy时报错RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3263483.html
Copyright © 2011-2022 走看看