codeforces 981D Bookshelves

http://www.elijahqi.win/archives/3533
D. Bookshelves
time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

Mr Keks is a typical white-collar in Byteland.

He has a bookshelf in his office with some books on it, each book has an integer positive price.

Mr Keks defines the value of a shelf as the sum of books prices on it.

Miraculously, Mr Keks was promoted and now he is moving into a new office.

He learned that in the new office he will have not a single bookshelf, but exactly k

k
bookshelves. He decided that the beauty of the k

k
shelves is the bitwise AND of the values of all the shelves.

He also decided that he won’t spend time on reordering the books, so he will place several first books on the first shelf, several next books on the next shelf and so on. Of course, he will place at least one book on each shelf. This way he will put all his books on k

k
shelves in such a way that the beauty of the shelves is as large as possible. Compute this maximum possible beauty.

Input
The first line contains two integers n

n
and k

k
(1≤k≤n≤50

1≤k≤n≤50
) — the number of books and the number of shelves in the new office.

The second line contains n

n
integers a1,a2,…an

a1,a2,…an
, (0< ai< 250

0< ai< 250
) — the prices of the books in the order they stand on the old shelf.

Output
Print the maximum possible beauty of k

k
shelves in the new office.

Examples
input

Copy
10 4
9 14 28 1 7 13 15 29 2 31
output

Copy
24
input

Copy
7 3
3 14 15 92 65 35 89
output

Copy
64
Note
In the first example you can split the books as follows:

(9+14+28+1+7)&(13+15)&(29+2)&(31)=24.

(9+14+28+1+7)&(13+15)&(29+2)&(31)=24.

In the second example you can split the books as follows:

(3+14+15+92)&(65)&(35+89)=64.

一开始并没有看到必须是从头开始连续的 耽误很长时间

具体解法:apio 巴厘岛雕塑简化版

按位贪心 设dp[i][j]表示前i个数字分j块 二进制这一位能否是1 转移就非常暴力即可

然后储存tmp 表示我前面已经确定好的几个位置中 都有哪几位 已经是1 了

然后做的时候参考代码 特判一些情况即可

#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if(T==S) return EOF;}
    return *S++;
}
inline ll read(){
    ll x=0,f=1;char ch=gc();
    while(!isdigit(ch)) {if (ch=='-') f=-1;ch=gc();}
    while(isdigit(ch)) x=x*10+ch-'0',ch=gc();
    return x*f;
}
const int N=100;
int n,K;ll a[N],ans,tmp,s[N];bool dp[N][N];
int main(){
//  freopen("d.in","r",stdin);
    n=read();K=read();
    for (int i=1;i<=n;++i) a[i]=read();
    for (int i=1;i<=n;++i) s[i]=s[i-1]+a[i];
    for (int k=60;~k;--k){
        memset(dp,0,sizeof(dp));dp[0][0]=1;
        for (int i=1;i<=n;++i){
            for (int j=1;j<=i;++j){
                for (int z=j-1;z<i;++z){
                    if (dp[z][j-1]&&((s[i]-s[z])&(1LL<<k))) 
                    if ((tmp&(s[i]-s[z]))>=tmp) dp[i][j]|=1;
                }
            }
        }if (dp[n][K]) tmp+=1LL<<k,ans+=1LL<<k;
        //for (int i=K;i<=n;++i) if(dp[n][i]) {break;}
    }printf("%I64d\n",ans);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值