CF 981D Bookshelves 按位贪心,DP

题意:长度为n的序列a,每个区间价值为:该区间内所有的数相加.
1<=k<=n<=50, 0<a[i]<2^50,现在将序列a分成k段,要求这k段价值的AND结果最大.

从最高位x开始贪心,初始ans=0.
check:若前n个分成k段 并且每段的和 sum & (ans+2^x) 那么使ans+2^x 继续判断.

判定时:设dp[i][j] 表示前i个分成j段 每段sum和ans+2^x 与运算是否为 ans+2^x. O(n^2*k*logS).

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=60+5;
ll n,k,a[N],p[N];
bool dp[N][N];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie();
	cin>>n>>k;
	for(int i=1;i<=n;i++)	cin>>a[i],p[i]=a[i]+p[i-1];
	ll res=0;
	for(int bit=60;bit>=0;bit--)
	{
		ll ned=res+(1ll<<bit);
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=n;i++)
		{
			if((p[i]&ned)==ned)
				dp[i][1]=true;
			for(int j=2;j<=k;j++)
			{
				for(int l=1;l<=i-j+1;l++)
				{
					ll sum=p[i]-p[i-l];
					if((sum&ned)==ned && dp[i-l][j-1])
						dp[i][j]=true;
				}
			}
		}
		if(dp[n][k]==true)
			res=ned;
	}
	cout<<res<<'\n';
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值