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;
}

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

  1. Codeforces - Avito Code Challenge 2018

    Portal A. Antipalindrome 暴力. B. Businessmen Problems 暴力. C. Useful Decomposition 居然不是C打头的?! 将一棵树划分成若 ...

  2. Avito Code Challenge 2018

    第一次打CF,很菜,A了三道水题,第四题好像是是数位DP,直接放弃了.rateing从初始的1500变成了1499,还是绿名,这就很尴尬.之后觉得后面的题目也没有想象的那么难(看通过人数)过两天吧剩下 ...

  3. Avito Code Challenge 2018 A~E

    A. Antipalindrome 还以为是什么神dp结果就是分情况讨论啊 原串是一串一样的字符的话输出0,是回文串的话输出n-1,否则直接输出原串长度 #include<iostream> ...

  4. cf掉分记——Avito Code Challenge 2018

    再次作死的打了一次cf的修仙比赛感觉有点迷.. 还好掉的分不多(原本就太低没法掉了QAQ) 把会做的前三道水题记录在这.. A: Antipalindrome emmmm...直接暴力枚举 code: ...

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

    题目链接:http://codeforces.com/contest/981/problem/G 题目大意: 有n个初始为空的‘魔法’可重集,向一个‘可重集’加入元素时,若该元素未出现过,则将其加入: ...

  6. Avito Code Challenge 2018 C

    C. Useful Decomposition time limit per test 1 second memory limit per test 256 megabytes input stand ...

  7. Avito Cool Challenge 2018 E. Missing Numbers 【枚举】

    传送门:http://codeforces.com/contest/1081/problem/E E. Missing Numbers time limit per test 2 seconds me ...

  8. Avito Cool Challenge 2018 C. Colorful Bricks 【排列组合】

    传送门:http://codeforces.com/contest/1081/problem/C C. Colorful Bricks time limit per test 2 seconds me ...

  9. Avito Cool Challenge 2018 B. Farewell Party 【YY】

    传送门:http://codeforces.com/contest/1081/problem/B B. Farewell Party time limit per test 1 second memo ...

随机推荐

  1. Adobe Flash player ActiveX下载地址

    http://www.adobe.com/support/flashplayer/downloads.html http://download.macromedia.com/pub/flashplay ...

  2. Codeforces Round #436 C. Bus

    题意:一辆车在一条路上行驶,给你路的总长度a,油箱的容量b,加油站在距离起点的距离f,以及需要走多少遍这条路k(注意:不是往返) 问你最少加多少次油能走完. Examples Input 6 9 2 ...

  3. C语言之标准头文件模板

    /*======================================================================================* * 版权 : xxx ...

  4. 阿里云-CentOS如何挂载硬盘

    阿里云CentOS挂载硬盘 查看当前未挂载的硬盘 # fdisk -l 创建硬盘分区 # fdisk /dev/vdb 根据提示,依次输入"n","p" &qu ...

  5. mysql mycat 中间件安装与使用

    一,什么是mycat 一个彻底开源的,面向企业应用开发的大数据库集群 支持事务.ACID.可以替代MySQL的加强版数据库 一个可以视为MySQL集群的企业级数据库,用来替代昂贵的Oracle集群 一 ...

  6. 什么是HTML?

    html是很多人编程的入门领域.作为初学者,不管你是在哪里学的,学校,视频教程,网络教程等等……它们都会告诉你HTML即:超文本标记语言(Hyper Text Markup Language).但第一 ...

  7. centos7下Etcd3集群搭建

    一.环境介绍 etcd主要功能是分布式的存储键值,优点不多说了,分布是集群,自动选举等等,自行百度,主要说下配置方法,折腾了几天,终于优点眉目了,记录下操作方法,本文参考了如下链接 https://w ...

  8. 理解javascript中的立即执行函数(function(){})()

    之前看了好多代码,都有用到这种函数的写法,但是都没认真的去想为什么会这样写,今天开始想学习下jquery的源码,发现jquery也是使用这种方式,用(function(window, undefine ...

  9. 解决InetAddress.isReachable(timeout)在windows xp始终返回false的bug

    笔者最近在做产品,其中一个环节用到ping测试主机是否在线. 开发环境:Windows 7 64bit+JDK1.8 x64 以下是检测主机是否在线,开发环境中测试通过 public static b ...

  10. JS继承(一)

    突然发现自己很久没写过什么东西了 其实从博客更新的速度上就可以看出一个人近期有没有成长 对 …… 我没有成长 也可以由此看出自己选择的企业是不是对的 对 …… 我不会离职…… 略略略 来咬我啊…… 于 ...