020 Bookshelves (CF981D)

7 篇文章 0 订阅
2 篇文章 0 订阅

题目链接:CF981D

  • 标签:按位、贪心、DP

  • 大意:有n本书,每本书都有一个价值a。将这n本书按照顺序连续地放在书架上(读题的时候忽略了“连续的”这个重要讯息,导致很长时间都没有思路),定义一个书架的美观程度为这个书架上所有书价值的总和,k个书架的美观程度为每个书架的按位与和,求这k个书架的最大美观程度。

  • 思路:先简单介绍一下按位与运算(自己对位运算不太熟悉老是碰壁)

    • 按位与(&):运算规则:只有两个数的二进制同时为1,结果才为1,否则为0。(负数按补码形式参加按位与运算)

      0 & 0= 0 ,0 & 1= 0,1 & 0= 0, 1 & 1= 1。

    • 借机补充一个常用的运算:移位运算:(<</>>)效果很简单,就是数向左/右移一位,左移时在最右端补0,右移时在最左端补1,由二进制的性质可知:
      n * 2 == (n << 1)
      n / 2 ==(n >> 1)
      1 << n == pow (2 , n)

    • 了解了按位与运算的特性后,我们不难由高位至低位进行贪心选择:如果最大位为1肯定要优于此位为0,而将多少个书放在一起,可以利用区间dp

    • dp[i][j]表示前i个书架放了j本书

  • 代码:

#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn=60;

LL dp[maxn][maxn],a[maxn];
LL sum[maxn],n,k;

int main() {
    cin>>n>>k;
    for(int i=1;i<=n;i++) {
        cin>>a[i];
        sum[i]=sum[i-1]+a[i];//计算前i项和 
    }
    LL ans=0;
    for(int bit=55;bit>=0;bit--) {//从最高位开始判断可否为1 
        LL num=(LL)1<<bit;//2的bit次方 (10000…0000) 
        memset(dp,0,sizeof(dp));
        dp[0][0] = 1;
         for(int l=1;l<=k;l++) {//放进k个书架里 
            for(int i=1;i<=n;i++) {//总共n本书 
                for(int j=0;j<i;j++) {//前一个书架放j本书 
                    if(dp[l-1][j]&&((sum[i]-sum[j])&num)&&(((sum[i]-sum[j])&ans)==ans))
					//如果前一个书架放j本书可行,且j+1~i号书之和(当前书架的和)的bit位是1,且不会令前置位丢1  
                        dp[l][i] = 1;//该位置可行 
                }
            }
        }
        if(dp[k][n]) ans+=num; 
    }
    cout<<ans;
}

(第一次发现原来插入代码块的时候是可以选择语言的,难怪我之前的代码不亮了)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。
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、付费专栏及课程。

余额充值