zoukankan      html  css  js  c++  java
  • bitset

    bitset就像一个bool类型(0/1)的数组一样,但是有空间优化——bitset中的一个元素一般只占1 bit,相当于一个char元素所占空间的八分之一。

    同时速度快,另外可以减少运算。如交集(and),并集(or),<< | 等,速度变为原来1/32(之前是每个int处理一次,1 bitset stores the information of 32 int)。

        bitset<10> b;///init 0
        b.set();    ///all is 1
        b.reset();  ///all is 0

        b.set(0,1); ///pos,value
        b.set(2,1);
        cout<<b<<endl;  ///0000000101 (0-9 从右到左 ; 左边是最高位)
        cout<<b.count()<<endl;///1的个数 2

        string str=b.to_string();///转为字符串 0001000001
        cout<<str<<endl;

        string s="10010";
        bitset<5> c(s); ///s is a string
        cout<<c<<endl;  ///10010

    寻找所有1

    for (i=tmp._Find_first();i<n;i=tmp._Find_next(i))

    1513 小Hi的烦恼

    注意空间

     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <cmath>
     4 #include <cstring>
     5 #include <string>
     6 #include <algorithm>
     7 #include <set>
     8 #include <map>
     9 #include <queue>
    10 #include <iostream>
    11 #include <bitset>
    12 using namespace std;
    13 
    14 #define ll long long
    15 
    16 const int maxn=3e4+10;
    17 const int inf=1e9;
    18 const double eps=1e-8;
    19 
    20 /*
    21 二维:树状数组
    22 k维:cdq分治 n^(k-1)*log(n)
    23 bitset:n^k/32
    24 */
    25 
    26 ///如何实现动态初始化
    27 bitset<maxn> a[maxn],t,tt;
    28 struct node
    29 {
    30     int x,y;
    31     bool operator<(const node &b) const
    32     {
    33         return x>b.x;
    34     }
    35 }f[5][maxn];
    36 
    37 int main()
    38 {
    39     int n,i,j,k;
    40     scanf("%d",&n);
    41 //    for (i=0;i<n;i++)
    42 //        a[i].set();
    43     for (i=0;i<n;i++)
    44         for (j=0;j<5;j++)
    45         {
    46             scanf("%d",&f[j][i].x);
    47             f[j][i].y=i;
    48         }
    49 
    50     ///也可以用string初始化,会快一点
    51     for (i=0;i<n;i++)
    52         tt.set(i,1);
    53     for (j=0;j<5;j++)
    54     {
    55         sort(f[j],f[j]+n);
    56 //        t.set();
    57         t=tt;
    58         for (i=0;i<n;i++)
    59         {
    60             k=f[j][i].y;
    61             t.set(k,0);
    62 //            a[k]&=t;
    63             if (j==0)
    64                 a[k]=t;
    65             else
    66                 a[k]&=t;
    67         }
    68     }
    69     for (i=0;i<n;i++)
    70         printf("%d
    ",a[i].count());
    71     ///其实也可以 a[k]|=not(t),再用n-a[i].count(),这样可以避免全1的初始化
    72     return 0;
    73 }
    74 /*
    75 
    76 */
     
  • 相关阅读:
    Java EE javax.servlet中的ServletContext接口
    Java EE javax.servlet中的ServletConfig接口
    MD5加密工具
    redis常见数据操作
    Java文件上传与下载
    JSP技术
    spring集成swagger
    freemarker模板引擎的使用
    log4j生成日志
    Java自定义注解
  • 原文地址:https://www.cnblogs.com/cmyg/p/10718197.html
Copyright © 2011-2022 走看看