CF981D Bookshelves

按位贪心+DP的好题qwq

首先看到题目的要求,统计价值的时候的操作是按位与,就要有按位分别计算的意识

开始没意识到结果想了好久还是看了题解才想到

由于统计价值的方式不是加和,所以可能会出现两个较大的值AND起来更小甚至变成0,所以不能直接DP

考虑按位贪心

显然高位为1后的值一定比高位不为1的值要大,所以从高位向下贪心,每次检查能否在使前i个位值不变的情况下,使新加入的位变为1

检查的时候用区间dp实现即可

\(dp[i][j]\)表示前j本书分到前i个书架上是否能使前面的位不变且当前检查的位为1

状态转移方程为:

dp[i][j]|=dp[i-1][kx]&&(((sum[j]-sum[kx])&&x)==x) 

总的复杂度是\(O(n^{2}k)\)

注意位运算优先级和枚举的最高位的位数

代码


#include <cstdio>
#include <cstring>
#include <algorithm>
#define int long long
using namespace std;
int n,k,a[51],sum[51],dp[51][51],ans=0;
int check(int x){
    memset(dp,0,sizeof(dp));
    dp[0][0]=1;
    for(int i=1;i<=k;i++)
        for(int j=1;j<=n;j++)
            for(int kx=0;kx<j;kx++)
                dp[i][j]|=dp[i-1][kx]&&(((sum[j]-sum[kx])&x)==x);
    return dp[k][n];
}
signed main(){
    scanf("%lld %lld",&n,&k);
    for(int i=1;i<=n;i++)
        scanf("%lld",&a[i]);
    for(int i=1;i<=n;i++)
        sum[i]=sum[i-1]+a[i];
    for(int i=60;i>=0;i--){
        int mid=ans|(1LL<<i);
        if(check(mid))
            ans|=(1LL<<i);
    }
    printf("%lld",ans);
    return 0;
}

转载于:https://www.cnblogs.com/dreagonm/p/9890518.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
38.He took his umbrella ______ it should rain.(2分) A in case of B in case C for fear D in time 39.Not until this term ( ) to realize how important this subject is to his future career as a diplomat.(2分) A he began B did he begin C he has begun D that he has begun 40.Great changes ______ in Shanghai since the beginning of the reform and opening-up policy.(2分) A took place B has taken place C has been taken place D have taken place 41.The larger the house is, ______.(2分) A the higher rent it is B the higher rent it would have C the higher the rent is D the higher rent it would be 42.Every time Jane has trouble ______ her car started, Sean will show up to lend a hand.(2分) A get B getting C to getting D to get44.Some bookshelves have been moved out of this office to make ______ for more computers.(2分) A space B place C room D position46. Wendy: Have you been to the new bakery on the corner? Arthur: NO, how is it? Wendy: It is heaven!________! (2分) A Their cakes are to strive for B Their cakes are to pay for C Their cakes are to struggle for D Their cakes are to die for 47.Dr. Jones, many students want to see you.( ) they wait here or outside?(2分) A Do B Will C Shall D Would 48.( ) who would like to go on the trip should put their names on the list. (2分) A Those B These C Somebody D The ones 49. Kate: Do you mind opening the door for me? Bob: _________. (2分) A It’s nothing B That’s all right C Yes, I’ll do it D Not at all 50.They first stop at a board _____ the menu is displayed.(2分) A when B while C where D which
最新发布
06-12

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值