zoukankan      html  css  js  c++  java
  • [ZJb417]区间众数

    题目大意:
      给定一个长度为$n(1leq nleq10^5)$的正整数序列$s(1leq s_ileq n)$,对于$m(1leq mleq10^)$次询问$l,r$,每次求区间$[s_l,ldots,s_r]$中,众数出现的次数以及众数的个数。

    思路:
      莫队。
      对于询问$l,r$,维护每个数$s_i$出现的次数$cnt1[i]$以及每个$cnt1[i]$出现的次数$cnt2[i]$。

     1 #include<cmath>
     2 #include<cstdio>
     3 #include<cctype>
     4 #include<algorithm> 
     5 inline int getint() {
     6     register char ch;
     7     while(!isdigit(ch=getchar()));
     8     register int x=ch^'0';
     9     while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
    10     return x;
    11 }
    12 const int N=100001,M=1000000;
    13 int a[N],belong[M],cnt1[N],cnt2[N],tmp,ans1[M],ans2[M];
    14 struct Query {
    15     int l,r,id;
    16     bool operator < (const Query &another) const {
    17         if(belong[l]==belong[another.l]) return belong[r]<belong[another.r];
    18         return belong[l]<belong[another.l];
    19     }
    20 };
    21 Query q[M];
    22 inline void ins(const int &x) {
    23     cnt2[cnt1[a[x]]++]--;
    24     cnt2[cnt1[a[x]]]++;
    25     tmp=std::max(tmp,cnt1[a[x]]);
    26 }
    27 inline void del(const int &x) {
    28     cnt2[cnt1[a[x]]--]--;
    29     cnt2[cnt1[a[x]]]++;
    30     while(!cnt2[tmp]) tmp--;
    31 }
    32 int main() {
    33     const int n=getint(),m=getint(),block=sqrt(n);
    34     for(register int i=1;i<=n;i++) {
    35         a[i]=getint();
    36         belong[i]=i/block;
    37     }
    38     for(register int i=0;i<m;i++) {
    39         const int l=getint(),r=getint();
    40         q[i]=(Query){l,r,i};
    41     }
    42     std::sort(&q[0],&q[m]);
    43     for(register int i=0,l=1,r=0;i<m;i++) {
    44         while(l<q[i].l) del(l++);
    45         while(l>q[i].l) ins(--l);
    46         while(r<q[i].r) ins(++r);
    47         while(r>q[i].r) del(r--);
    48         ans1[q[i].id]=tmp;
    49         ans2[q[i].id]=cnt2[tmp];
    50     }
    51     for(register int i=0;i<m;i++) {
    52         printf("%d %d
    ",ans1[i],ans2[i]);
    53     }
    54     return 0;
    55 }
  • 相关阅读:
    django 项目 crm 关于展示表的细节
    djagngo crm 项目 展示页面 和分页
    django项目 crm登录 注册
    django项目 crm表结构一些常用的字段
    django认证 auth
    django form组件
    json和ajax技术
    VS 2013编译64位版本QT 4.8.6及使用cmake为依赖QT生成VS项目时Could NOT find Qt4
    使用国内pypi源来安装python包
    [转]ubuntu 下无法启动chrome
  • 原文地址:https://www.cnblogs.com/skylee03/p/8423363.html
Copyright © 2011-2022 走看看