zoukankan      html  css  js  c++  java
  • P2326 AKN’s PPAP

    P2326 AKN’s PPAP
    比较裸的贪心
    从高位向下枚举,如果当前位为1的个数大于1,ans+=(1<<i),然后从这些数中再向下枚举。

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<algorithm>
    #include<cmath>
    #include<ctime>
    #include<cstring>
    #define inf 2147483647
    #define For(i,a,b) for(register int i=a;i<=b;i++)
    #define p(a) putchar(a)
    #define g() getchar()
    //by war
    //2017.10.25
    using namespace std;
    int n;
    int ans;
    queue<int>q[30];
    int t;
    int a[100010];
    int cnt; 
    int temp[100010];
    int T;
    void in(int &x)
    {
        int y=1;
        char c=g();x=0;
        while(c<'0'||c>'9')
        {
        if(c=='-')
        y=-1;
        c=g();
        }
        while(c<='9'&&c>='0')x=x*10+c-'0',c=g();
        x*=y;
    }
    void o(int x)
    {
        if(x<0)
        {
            p('-');
            x=-x;
        }
        if(x>9)o(x/10);
        p(x%10+'0');
    }
    
    void bl(int now)
    {
        //ÓÃÊý×éÀ´´æ 
        int top;
        cnt=0;
        while(q[now+1].size()>0)
        {
            top=q[now+1].front();
            /*if((top>>i)&1==1)
            q[i].push(top);*/
            q[now+1].pop();
            temp[++cnt]=top;
        }
        for(register int i=now;i>=0;i--)
        {
                For(j,1,cnt)
                if((temp[j]>>i)&1==1)
                q[i].push(temp[j]);    
                if(q[i].size()>1)
                {
                ans+=(1<<i);
                if(i>0)
                bl(i-1);
                break;    
                }
        }
    }
    
    int main()
    {
        in(t);
        T=t;
        while(t--)
        {
            ans=0;
            in(n);
            For(i,1,n)
            in(a[i]);
            for(register int i=20;i>=0;i--)
            {
                For(j,1,n)
                if((a[j]>>i)&1==1)
                q[i].push(a[j]);    
                if(q[i].size()>1)
                {
                ans+=(1<<i);
                if(i>0)
                bl(i-1);
                break;    
                }    
            }
            printf("Case #%d: %d ",T-t,ans);
            p('
    ');//o(ans),
            For(i,0,20)
            while(q[i].size()>0)
            q[i].pop();
            
        }
        
         return 0;
    }
  • 相关阅读:
    Linux用户、用户组、文件权限设置
    spring,springMvc和mybatis整合配置
    spring,springMvc和hibernate整合
    spring与mybatis
    spring与Dbcp
    初识事物
    spring与Aop
    初识spring
    mysql 完整性约束
    mysql数据库的基本操作
  • 原文地址:https://www.cnblogs.com/war1111/p/7729566.html
Copyright © 2011-2022 走看看