CodeForces 981D Bookshelves

题目:点击打开链接

题意:给你n本书以及每本书的权值,现在让你把n本书放到k个书架上(只有连续的几本书可以放到一个书架上),每个书架的权值是书架上每本书的权值加和,总的"beauty"是每个书架权值按位与的结果,要求输出最大的"beauty"。

分析:因为要求总"beauty"是所有书架权值的按位与的结果,所以对于总"beauty"的二进制,位数越高的1的价值越大,所以可以从高位开始枚举这一位是否能取1,如果能取一定可以更新答案。初始ans为0,每枚举一位,判断ans|1<

代码:

#pragma comment(linker, "/STACK:102400000,102400000")///ÊÖ¶¯À©Õ»
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define debug test
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define inf 0x3f3f3f3f
#define eps 1e-10
#define MOD 1000000007
#define PI acos(-1.0)
const int N = 3e5+10;

ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
int to[4][2]={{-1,0},{1,0},{0,-1},{0,1}};

int n,k;
ll a[60];
bool dp[100][100];

bool ck(ll 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 l=1;l<=j;l++)
                if( ((a[j]-a[l-1])&x)==x && dp[i-1][l-1] ) dp[i][j]=1;
    ///注意&的优先级低于==
    return dp[k][n];
}

int main() {
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>k;
    for(int i=1;i<=n;i++) cin>>a[i],a[i]+=a[i-1];
    ll ans=0;
    for(ll i=1LL<<60;i>0;i>>=1)
        if( ck(ans|i) ) ans |= i;
    cout<


你可能感兴趣的:(ACM,codefoces)