码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces #460D Little Victor and Set 构造

时间:2015-05-13 12:56:12      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:codeforces   构造   

题目大意:给定一个区间[l,r],你需要在这个区间中选择最多k个不同的数,使得异或和最小

r?l+14时,暴力枚举集合即可
r?l+15时,讨论:
k4,则[l,r]中一定存在一组数为2k,2k+1,2k+2,2k+3,故答案为0
k=1,则只能取l
k=2,则只能取2k,2k+1,异或值为1
k=3,由于我们可以只取2k,2k+1,因此答案上界不会超过1
我们只需要判断能否在[l,r]区间内找到3个不同的数使得异或值为0即可
不妨设这三个数从小到大分别为x,y,z
首先最高位一定是这样:
z 1...
y 1...
x 0...
然后我们讨论次高位,一共有三种情况:
z 11...
y 10...
x 01...

z 11...
y 11...
x 00...

z 10...
y 10...
x 00...
容易证明,如果选择方案2或方案3存在一组合法的解,那么将每个数的次高位删除,用最高位代替次高位,一定也是一组合法的解
那么次高位一定是这样:
z 11...
y 10...
x 01...
现在三个数的大小关系都确定了,我们应该让最小的数尽量大,最大的数尽量小,这样才能让这三个数尽量在[l,r]区间内
那么显然要这么搞:
z 11000000...
y 10111111...
x 01111111...
然后就简单了,我们只需要找到第一个lx,判断对应的z是否r即可

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
long long l,r,k,ans=0x3f3f3f3f3f3f3f3fll;
int Count(int x)
{
    int re=0;
    for(;x;x-=x&-x)
        ++re;
    return re;
}
int main()
{
    long long i,j;
    cin>>l>>r>>k;
    if(r-l+1<=4)
    {
        int _ans;
        for(i=1;i<1<<r-l+1;i++)
            if(Count(i)<=k)
            {
                long long temp=0;
                for(j=0;j<r-l+1;j++)
                    if( i&(1<<j) )
                        temp^=l+j;
                if(temp<=ans)
                    ans=temp,_ans=i;
            }
        cout<<ans<<endl;
        cout<<Count(_ans)<<endl;
        for(j=0;j<r-l+1;j++)
            if( _ans&(1<<j) )
                cout<<l+j<<‘ ‘;
        return 0;
    }
    if(k>=4)
    {
        if(l&1) ++l;
        cout<<0<<endl;
        cout<<4<<endl;
        for(i=0;i<4;i++)
            cout<<l+i<<‘ ‘;
        return 0;
    }
    if(k>=3)
    {
        for(i=1;i<l;i=i<<1|1);
        if( ( (i+1) | (i+1>>1) )<=r )
        {
            cout<<0<<endl;
            cout<<3<<endl;
            cout<<i<<‘ ‘<<( (i+1) | (i>>1) )<<‘ ‘<<( (i+1) | (i+1>>1) )<<‘ ‘;
            return 0;
        }
    }
    if(k>=2)
    {
        if(l&1) ++l;
        cout<<1<<endl;
        cout<<2<<endl;
        cout<<l<<‘ ‘<<l+1<<‘ ‘;
        return 0;
    }
    cout<<l<<endl;
    cout<<1<<endl;
    cout<<l<<‘ ‘;
    return 0;
}

codeforces #460D Little Victor and Set 构造

标签:codeforces   构造   

原文地址:http://blog.csdn.net/popoqqq/article/details/45690941

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!