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. express 内存溢出问题分析定位

    一.现象 1. 如下报错 FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 1: n ...

  2. CR--同事分享学习

    1.持续发布是什么? 频繁地将软件的新版本,交付给质量团队或者用户,以供评审. 2.对接持续发布需要具备什么条件? 1)  测试自动化程度较高,持续发布的终态是不需要人工介入 2)  做到持续集成,持 ...

  3. Mybatis useGeneratedKeys 填充自增主键值(使用Mysql)的原理分析

    一.Mybatis配置 <insert id="insert" parameterType="com.test.TestDO" keyProperty=& ...

  4. php 安装最新的redis连接扩展

    用于与redis连接的Php扩展[RC表示公测,我们用的是不带RC的稳定版本]下载包地址:http://pecl.php.net/package/redis 最新稳定版本:4.3.0 下载包:# wg ...

  5. python 并发编程 操作系统 进程 并发.并行 及 同步/异步,阻塞/非阻塞

    操作系统: 多道技术背景: 提高工作效率(充分利用IO阻塞的时间) 同时执行多个任务 多道技术 空间复用:充分的利用内存空间 时间复用:充分利用IO阻塞时间 分时系统: 并发:提高了程序的工作效率 两 ...

  6. ALV编辑数据后未更新到内表

    在调用 ‘I_CALLBACK_USER_COMMAND’的时候定义 DATA: GR_ALV TYPE REF TO CL_GUI_ALV_GRID. "alv数据更改后更新到内表 CAL ...

  7. appium 移动端自动化测试一

    最近公司决定使用appium为主体框架做一个移动端得自动化测试系统,我会陆续记录项目得进展. 1.首先是appium环境得搭建, 需要安装appium-server ,appium-desktop, ...

  8. 牛客小白月赛13 小A的回文串(Manacher)

    链接:https://ac.nowcoder.com/acm/contest/549/B来源:牛客网 题目描述 小A非常喜欢回文串,当然我们都知道回文串这种情况是非常特殊的.所以小A只想知道给定的一个 ...

  9. C#使用反射获取对象变化的情况

    记录日志时, 经常需要描述对象的状态发生了怎样的变化, 以前处理的非常简单粗暴: a. 重写class的ToString()方法, 将重要的属性都输出来 b. 记录日志时:  谁谁谁  由  变更前实 ...

  10. 11.Redis缓存设计

    11.Redis缓存设计11.1 缓存的收益和成本11.2 缓存更新策略11.3 缓存粒度控制11.4 穿透优化11.5 无底洞优化11.6 雪崩优化11.7 热点key重建优化11.8 本章重点回顾