Codeforces Avito Code Challenge 2018 D. Bookshelves

Codeforces Avito Code Challenge 2018 D. Bookshelves

题目连接:

http://codeforces.com/contest/981/problem/D

Description

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$ bookshelves. He decided that the beauty of the $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$ shelves in such a way that the beauty of the shelves is as large as possible. Compute this maximum possible beauty.

Sample Input

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

Sample Output

24

题意

有n个数字,将他们分成k组,组内求和,组间求按位与,问结果最大是多少

There are n numbers, devide them into k groups. Sum the numbers of each group, and & them. Output the maximum possible answer.

题解:

按位贪心。假设答案为ans,dp[i][j]表示前i个分成j组能不能构成ans。

Greedy for bigger answer. Assume that ans is the answer, dp[i][j] represent j groups which is made by former i numbers can construct ans or not.

代码

#include <bits/stdc++.h>

using namespace std;

int n, k;
long long a[100];
long long dp[100][100];
long long sum[100];
long long ans;

long long check(long long num) {
    memset(dp, 0, sizeof dp);
    for (int i = 1; i <= n; i++) {
        if ((sum[i] & num) == num) dp[i][1] = 1;
        for (int j = 1; j < i; j++) {
            if (((sum[i] - sum[j]) & num) != num) continue;
            for (int o = 2; o <= min(k, i); o++) dp[i][o] |= dp[j][o - 1];
        }
    }
    return dp[n][k];
}

int main() {
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= n; i++)
        sum[i] = sum[i - 1] + a[i];

    for (int p = 63; p >= 0; p--) {
        if (check(ans|(1<<p)))
            ans |= (1 << p) ;
    }

    cout << ans << endl;
}

原文地址:https://www.cnblogs.com/EDGsheryl/p/9165318.html

时间: 2024-10-07 08:33:15

Codeforces Avito Code Challenge 2018 D. Bookshelves的相关文章

cf掉分记——Avito Code Challenge 2018

再次作死的打了一次cf的修仙比赛感觉有点迷.. 还好掉的分不多(原本就太低没法掉了QAQ) 把会做的前三道水题记录在这.. A: Antipalindrome emmmm...直接暴力枚举 code: //By Menteur_Hxy #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace std; int n,ans; cha

[Avito Code Challenge 2018 G] Magic multisets(线段树)

题目链接:http://codeforces.com/contest/981/problem/G 题目大意: 有n个初始为空的‘魔法’可重集,向一个‘可重集’加入元素时,若该元素未出现过,则将其加入:否则该可重集中所有元素的个数都会翻倍. 例如将$2$加入${1,3}$会得到${1,2,3}$,将$2$加入${1,2,3,3}$会得到${1,1,2,2,3,3,3,3}$. $q$次操作,每次操作要么向一个区间内的所有可重集加入某个元素,要么询问一个区间内可重集的大小之和. $n,q ≤ 2×1

Avito Cool Challenge 2018:C. Colorful Bricks

C. Colorful Bricks 题目链接:https://codeforces.com/contest/1081/problem/C 题意: 有n个横向方块,一共有m种颜色,然后有k个方块的颜色与其左边的颜色不同(第一个除外),问一共有多少染色方案. 题解: 我们首先来考虑一下dp. 设dp(i,j)为当前第i个方块,一共有j个方块与它前面的方块不同的方案个数. 那么转移方程为dp(i,j)=dp(i-1,j-1)*(m-1)+dp(i-1,j). 代码如下: #include <bits

Avito Cool Challenge 2018 A. B题解

A. Definite Game 题目链接:https://codeforces.com/contest/1081/problem/A 题意: 给出一个数v,然后让你可以重复多次减去一个数d,满足v%d!=0,问最后可以得到最小的是多少. 题解: 除开v=2输出2,其余直接输出1就行了= =/ 代码如下: #include <bits/stdc++.h> using namespace std; int main(){ int v; cin>>v; cout<<(v==

codeforces Intel Code Challenge Final Round (div.1 + div.2 combined)

比赛水掉3题rk559 rating+115 赛后切掉C n年没打cf了终于又重新变蓝了,果然太弱... 1.A题  Checking the Calendar 给定两个星期几,问是否可能分别是两个月的第一天. 水题暴力枚举月份 #include<cstdio> #include<cstring> #include<cstdlib> #include<vector> #include<algorithm> #include<function

Avito Cool Challenge 2018 B - Farewell Party

题目大意: 有n个人 接下来一行n个数a[i] 表示第i个人描述其他人有a[i]个的帽子跟他不一样 帽子编号为1~n 如果所有的描述都是正确的 输出possible 再输出一行b[i] 表示第i个人的帽子的编号 如果存在矛盾 输出impossible 如果存在p 个人都描述有q个人跟他们的帽子不一样 此时若 p+q=n 说明正确且这p个人的帽子都一样 如 a[] = 3 3 2 2 2 ,此时一种解为 b[] = 1 1 2 2 2 存在p=2个人描述有q=3个人跟他们不一样 说明这两个人的帽子

Avito Cool Challenge 2018:D. Maximum Distance (最小生成树)

题目链接 题意 : 给出一个联通图和一些特殊的点,现在定义cost(u,v)为一条从u到v的路径上面边权的最大值 , 定义dis(u,v) 为从u到v 路径上面cost 的最小值 然后求所有特殊点到其他特殊点的最大距离 题解: 做这题前,首先思考一件事情,对于一颗树来说点到点的距离是不是就是树上面路径的边权最大值 我们来证明一下:假设在最小生成树上面的路径cost为w1,另外在原图中还有一条路径从u到v,其cost为w2,那么必然有w2>w1的.那么我们最后的dis一定是w1. 那么我们现在的目

Avito Cool Challenge 2018 Solution

A. Definite Game 签. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int main() 5 { 6 int a; 7 while (scanf("%d", &a) != EOF) 8 { 9 [](int x) 10 { 11 for (int i = x - 1; i >= 1; --i) if (x % i) 12 { 13 printf("%d\n",

Codeforces Zepto Code Rush 2014 Feed with Candy 贪心

A. Feed with Candy time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The hero of the Cut the Rope game is a little monster named Om Nom. He loves candies. And what a coincidence! He also is