• Avito Code Challenge 2018 D. Bookshelves


    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 kk bookshelves. He decided that the beauty of the kk 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 kk 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 nn and kk (1kn501≤k≤n≤50) — the number of books and the number of shelves in the new office.

    The second line contains nn integers a1,a2,ana1,a2,…an, (0<ai<2500<ai<250) — the prices of the books in the order they stand on the old shelf.

    Output

    Print the maximum possible beauty of kk 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.

     

    题意:

                给 n 个数  ,要求将 n 个数分为 k 个区域, 使每个区域元素和的and和最大,求最大值。

    思路:既然是and和最大,那么我们从最高位开始枚举 答案该位为 1 的时候,然后通过dp转移一哈。

     

    过题代码

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=70;
    typedef long long ll;
    ll sum[maxn],n,k,x,ans=0;
    bool dp[maxn][maxn];
    int main(){
        std::ios::sync_with_stdio(false);
        std::cin.tie();
        cin>>n>>k;
        sum[0]=0;
        for (int i=1; i<=n; i++) cin>>x,sum[i]=sum[i-1]+x;
        for (int p=60; p>=0; p--){
            memset(dp,false,sizeof(dp));
            ll a=(ans|(1<<p));
            for (int i=1; i<=n; i++)
                for (int j=1; j<i; j++){
                    if(j==1) dp[i][j]=((sum[i]&a)==a);
                    else
                        for (int s=1; s<i; s++)
                            if(dp[s][j-1])
                                dp[i][j]|=(((sum[i]-sum[s])&a)==a);
                }
            if(dp[n][k]) ans=a;
        }
        cout<<ans<<endl;
        return 0;
    }
    
  • 相关阅读:
    zkw费用流
    luogu5212/bzoj2555 substring(后缀自动机+动态树)
    后缀数据结构模板2
    后缀数据结构模板1
    通用动态树(Link-Cut Tree)模板
    上下界网络流总结
    多项式多点求值
    拉格朗日反演
    多项式板子·新
    luogu2387 [NOI2014]魔法森林
  • 原文地址:https://www.cnblogs.com/acerkoo/p/9490326.html
Copyright © 2020-2023  润新知