C/C++教程

【刷题】cf D. Binary Spiders

本文主要是介绍【刷题】cf D. Binary Spiders,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Binary Spiders are species of spiders that live on Mars. These spiders weave their webs to defend themselves from enemies.

To weave a web, spiders join in pairs. If the first spider in pair has xx legs, and the second spider has yy legs, then they weave a web with durability x⊕yx⊕y. Here, ⊕⊕ means bitwise XOR.

Binary Spiders live in large groups. You observe a group of nn spiders, and the ii-th spider has aiai legs.

When the group is threatened, some of the spiders become defenders. Defenders are chosen in the following way. First, there must be at least two defenders. Second, any pair of defenders must be able to weave a web with durability at least kk. Third, there must be as much defenders as possible.

Scientists have researched the behaviour of Binary Spiders for a long time, and now they have a hypothesis that they can always choose the defenders in an optimal way, satisfying the conditions above. You need to verify this hypothesis on your group of spiders. So, you need to understand how many spiders must become defenders. You are not a Binary Spider, so you decided to use a computer to solve this problem.

  思路:来自: Codeforces Round #765 (Div. 2)A-D_Arctic_Clam的博客-CSDN博客

 

代码:至今还在wa...

#include<cstdio>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
inline int read()
{
    int x=0,f=1;char c=getchar();
    while(c<'0' || c>'9' ) 
    {
        if(c=='-' ) f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9' ) x=(x<<3)+(x<<1)+c-'0',c=getchar();
    return x*f;
}

int n,k;
const int N=3e5+10;
int a[N],mod=1;

struct node
{
    int pre,lst,pos;
    bool operator < (const node &a ) const
    { return pre<a.pre ; }
}d[N];
vector <int > Ans;

int ans=0;
int val[30*N],tol;
int ch[30*N][2];
void clear()
{
    for(int i=0;i<=tol;i++)
        ch[i][0]=ch[i][1]=0;
    tol=1;
}
void build(int x) 
{
    int u=0;
    for(int i=30;i>=0;i--) 
    {
        int v=(x>>i)&1;
        if(!ch[u][v]) 
        { 
            ch[tol][0]=ch[tol][1]=0; 
            val[tol]=0; 
            ch[u][v]=tol++; 
        }
        u=ch[u][v]; 
    }
    val[u]=x;
}
int query(int x)
{ 
    int u=0;
    for(int i=30;i>=0;i--)
    {
        int v=(x>>i)&1;
        if(ch[u][v^1]) u=ch[u][v^1];
        else u=ch[u][v];
    }
    //printf("q:%d %d\n",x,val[u]);
    return val[u];
}
void work(int l,int r)
{
    //printf(" %d %d\n",l ,r );
    if(r==l ) 
    {
        ans++;
        //printf("%d\n",ans);
        Ans.push_back(d[l].pos );
        return ;
    }
    
    clear();
    for(int i=l;i<=r;i++)
    //    printf(" b::%d\n",d[i].lst ),
        build(d[i].lst );
    for(int i=l;i<r;i++)
    {
        if(query(d[i].lst ) >=k )
        {
            for(int j=i+1;j<=r;j++)
                if((d[i].lst ^ d[j].lst ) >=k )
                {
                    Ans.push_back(d[i].pos );
                    Ans.push_back(d[j].pos );
                    ans+=2;
                    //printf("%d %d %d\n",ans,d[j].lst,d[i].lst  );
                    return ;
                }
        }
    }
    ans++;
    Ans.push_back(d[l].pos );
}

int main()
{
    n=read(),k=read();
    while(mod<=k ) mod<<=1;
    for(int i=1;i<=n;i++)
    {
        a[i]=read(); d[i].pos =i;
        d[i].pre =a[i]/mod,d[i].lst =a[i]%mod;
    }
    sort(d+1,d+n+1);
    
    if(k==0 )
    {
        printf("%d\n",n);
        for(int i=1;i<=n;i++) printf("%d ",i);
        return 0; 
    }
    
    int st=1;
    for(int i=1;i<=n;i++)
        if(d[i].pre !=d[st].pre ) 
            work(st,i-1),st=i;
    work(st,n);
    
    if(ans>=2 )
    {
        printf("%d\n",ans);
        int sz=Ans.size();
        for(int i=0;i<sz;i++)
            printf("%d ",Ans[i]);
    }
    else printf("-1");
    return 0;
} 
View Code

 

 

这篇关于【刷题】cf D. Binary Spiders的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!