多校联合第二场 1005 卡片翻转

http://acm.hdu.edu.cn/showproblem.php?pid=4876

Problem Description
ZCC loves playing cards. He has n magical cards and each has a number on it. He wants to choose k cards and place them around in any order to form a circle. He can choose any several  consecutive cards the number of which is m(1<=m<=k) to play a magic. The magic is simple that ZCC can get a number x=a1⊕a2...⊕am, which ai means the number on the ith card he chooses. He can play the magic infinite times, but  once he begin to play the magic, he can’t change anything in the card circle including the order.
ZCC has a lucky number L. ZCC want to obtain the number L~R by using one card circle. And if he can get other numbers which aren’t in the range [L,R], it doesn’t matter. Help him to find the maximal R.
 

Input
The input contains several test cases.The first line in each case contains three integers n, k and L(k≤n≤20,1≤k≤6,1≤L≤100). The next line contains n numbers means the numbers on the n cards. The ith number a[i] satisfies 1≤a[i]≤100.
You can assume that all the test case generated randomly.
 

Output
For each test case, output the maximal number R. And if L can’t be obtained, output 0.
 

Sample Input
  
  
4 3 1 2 3 4 5
 

Sample Output
  
  
7
Hint
⊕ means xor

题意:选k个数,排成一个环,从这个环上取连续的几个数,得到他的异或值,求出最大的r,使l-r这些数都能异或出来,如果l<r输出0.

         有个关键地方要剪枝,不然就TLE了,就是随机取了k个数之后,看能不能满足l-r,如果随机取的且没有按顺序都没满足,如果这个就不要排序再异或取值了。

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
int n,m,k;
int s[100005],a[100005],b[100005];
int erfen(int x)
{
    int l=0,r=n;
    int mid,ss=0;;
    while(l<=r)
    {
        mid=(l+r)/2;
        if(s[mid]<x)
        {
             ss=mid;
             l=mid+1;
        }
        else
            r=mid-1;
    }
    return ss;
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        memset(a,0,sizeof(a));
        memset(s,0,sizeof(s));
        memset(b,0,sizeof(b));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            s[i]=s[i-1]+a[i];
        }
        for(int i=1;i<=m;i++)
            scanf("%d",&b[i]);
        b[++m]=2e9;
        int sum=0,p=0,temp;
        for(int i=1;i<=m;i++)
        {
            if(n-p<b[i]-sum)
            {
                sum+=n-p;
                break;
            }
            else
            {
                temp=p+b[i]-sum-1;
                sum+=b[i]-sum;
                p=erfen(s[temp]-k);
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值