ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【刷题】cf D. Binary Spiders

2022-01-15 01:04:16  阅读:220  来源: 互联网

标签:Binary ch int defenders Ans cf spiders ans 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

 

 

 

标签:Binary,ch,int,defenders,Ans,cf,spiders,ans,Spiders
来源: https://www.cnblogs.com/xwww666666/p/15805957.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有