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;
}
Codeforces Avito Code Challenge 2018 D. Bookshelves的更多相关文章
- Codeforces - Avito Code Challenge 2018
Portal A. Antipalindrome 暴力. B. Businessmen Problems 暴力. C. Useful Decomposition 居然不是C打头的?! 将一棵树划分成若 ...
- Avito Code Challenge 2018
第一次打CF,很菜,A了三道水题,第四题好像是是数位DP,直接放弃了.rateing从初始的1500变成了1499,还是绿名,这就很尴尬.之后觉得后面的题目也没有想象的那么难(看通过人数)过两天吧剩下 ...
- Avito Code Challenge 2018 A~E
A. Antipalindrome 还以为是什么神dp结果就是分情况讨论啊 原串是一串一样的字符的话输出0,是回文串的话输出n-1,否则直接输出原串长度 #include<iostream> ...
- cf掉分记——Avito Code Challenge 2018
再次作死的打了一次cf的修仙比赛感觉有点迷.. 还好掉的分不多(原本就太低没法掉了QAQ) 把会做的前三道水题记录在这.. A: Antipalindrome emmmm...直接暴力枚举 code: ...
- [Avito Code Challenge 2018 G] Magic multisets(线段树)
题目链接:http://codeforces.com/contest/981/problem/G 题目大意: 有n个初始为空的‘魔法’可重集,向一个‘可重集’加入元素时,若该元素未出现过,则将其加入: ...
- Avito Code Challenge 2018 C
C. Useful Decomposition time limit per test 1 second memory limit per test 256 megabytes input stand ...
- Avito Cool Challenge 2018 E. Missing Numbers 【枚举】
传送门:http://codeforces.com/contest/1081/problem/E E. Missing Numbers time limit per test 2 seconds me ...
- Avito Cool Challenge 2018 C. Colorful Bricks 【排列组合】
传送门:http://codeforces.com/contest/1081/problem/C C. Colorful Bricks time limit per test 2 seconds me ...
- 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 ...
随机推荐
- 微信小程序:request合法域名检验出错,https://apis.map.qq.com 不在以下 request 合法域名列表中
设置域名 登录微信小程序后台, 设置→开发设置→服务器设置 必须设置域名,微信小程序才能进行网络通讯,不然会报错 如果没有设置合法域名,在开发阶段是可以不设置合法域名的 详情 -项目设置 好了,完美解 ...
- CSRF 和 XSS 的区别
XSS 利用的是用户对指定网站的信任,CSRF 利用的是网站对用户网页浏览器的信任 XSS: 跨站脚本攻击 原名为Cross Site Scriptin,为避免和网页层级样式表概念混淆, 另名为XSS ...
- python基础之socket编程
一 客户端/服务器架构 二 osi七层 三 socket层 四 socket是什么 五 套接字发展史及分类 六 套接字工作流程 七 基于TCP的套接字 八 基于UDP的套接字 九 粘包现象 十 什么是 ...
- py_innodb_page_info
python py_innodb_page_info.py -v /usr/local/var/mysql/ibdata1 mylib.py #encoding=utf-8 import os imp ...
- TZOJ 5694 区间和II(树状数组区间加区间和)
描述 给定n个整数,有两个操作: (1)给某个区间中的每个数增加一个值: (2)查询某个区间的和. 输入 第一行包括两个正整数n和q(1<=n, q<=100000),分别为序列的长度和操 ...
- 监听端口,获取webService请求报文
第一步下载我们的wsdl文件到本地 第二步建立一个测试webservice工程,把wsdl放在项目里面 第三步把测试webservice中的wsdlLocation改成localhost.....你的 ...
- shell执行Python并传参
shell: python test.py a1 222 test.py import sys print(sys.argv[1], type(sys.argv[1])) # a1 str print ...
- 关于项目里server清楚缓存的代码
Venk proc存在很多问题,不能应对高并发的情况,所以提供了这个 方法来清理cache, 但是前提是需要有prod的权限: 要想验证是否通过URL清楚了缓存,就要 removeCache url执 ...
- LoadRunner如何监控Linux下的系统资源
1. 安装rsh,rsh-server [root@localhost /]# yum install rsh [root@localhost /]# yum install rsh-server 或 ...
- Linux学习笔记:重定向>和>>
本文参考 https://www.cnblogs.com/piperck/p/6219330.html 重定向命令 > 和 >> 将命令的输出结果存储在指定文件中. 例如下面的例子就 ...