DP题组
按照顺序来。
大意:
给你一个集合,求其所有非空子集的权值的中位数。
某集合的权值即为其元素之和。
1 <= n <= 2000
解:
集合配对,每个集合都配对它的补集。
最大的那个没有配对,所以求(原集合的权值 + 1) >> 1,不小于这个的第一个即为所求。
用bitset实现可行性背包。
#include <cstdio>
#include <bitset> const int N = ; std::bitset<N * N> bt; int a[N]; int main() {
int n, tot = ;
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
}
for(int i = ; i <= n; i++) {
bt |= (bt << a[i]);
bt[a[i]] = ;
tot += a[i];
}
tot = (tot + ) >> ;
while(bt[tot] == ) {
tot++;
}
printf("%d", tot);
return ;
}
AC代码
大意:
输入 N 个数字和 K 。
如果对于所有包含 Ai ,且和不小于 K 的集合,去掉 Ai 后和还不小于 K ,那么 Ai 就是 no need 的。
问有多少个元素是 no need 的。
1 <= n, k <= 5000
解:
no need 的一定是连续最小的一段,证明如下:
若 x need,且 y > x
对于每个包含 x 且 >= K 的集合,去掉 x 一定小于 K 。
若此集合包含 y ,则去掉 y 也小于 K 。
若此集合不包含 y ,则把 x 换成 y 即可。
这样证明了所有含x的集合。
对于某些把 y 换成 x 就会小于 K 的集合,
把 y 换成 0 也小于 K 。
证毕。
然后二分check。
check函数用上一题的思想,对于 x ,只要去掉 x 的集合和没有在[k - x, k)之间的,x 即为 no need
若 x >= k,单 x 元素即为所求,x 为 need。
#include <cstdio>
#include <bitset>
#include <algorithm> const int N = ; std::bitset<N> bt; int n, a[N], k; inline bool check(int x) {
if(a[x] >= k) {
return ;
}
bt.reset();
for(int i = ; i <= n; i++) {
if(i == x) {
continue;
}
if(a[i] > N - ) {
break;
}
bt |= (bt << a[i]);
bt.set(a[i]);
}
for(int i = k - a[x]; i < k; i++) {
if(bt[i]) {
return ;
}
}
return ;
} int main() {
scanf("%d%d", &n, &k);
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
}
std::sort(a + , a + n + );
int l = , r = n, mid;
while(l < r) {
mid = (l + r + ) >> ;
if(check(mid)) {
r = mid - ;
}
else {
l = mid;
}
}
if(r == && check()) {
r = ;
}
printf("%d", r);
return ;
}
AC代码
大意:
在 n * m 的区域中,输入 k 个黑格子的位置(x,y)。
对于每个 3 * 3 的区域,会包含 0 到 9 个黑格子。
求包含 0 ~ 9 个黑格子的 3 * 3 的区域各有多少个?
1 <= m, n <= 10 ^ 9
0 <= k <= 10 ^ 5
解:
每个黑格子只会对 9 个 3 * 3 的区域产生影响。
注意map的用法: it -> second
#include <cstdio>
#include <map>
#include <algorithm> typedef long long LL; const int N = ; struct A {
int x, y;
A(int x = , int y = ) {
this->x = x;
this->y = y;
}
bool operator < (const A &d) const {
if(x == d.x) {
return y < d.y;
}
return x < d.x;
}
bool operator == (const A &d) const {
return x == d.x && y == d.y;
}
}a[N * ]; std::map<A, int> mp; int ans[]; int main() {
int m, n, k;
scanf("%d%d%d", &n, &m, &k);
for(int i = , x, y; i <= k; i++) {
scanf("%d%d", &x, &y);
for(int j = ; j < ; j++) {
for(int k = ; k < ; k++) {
if(x + j > n || y + k > m || x + j < || y + k < ) {
continue;
}
mp[A(x + j, y + k)]++;
}
}
}
std::map<A, int>::iterator it = mp.begin();
int tot = ;
for(; it != mp.end(); it++) {
tot++;
ans[it->second]++;
}
printf("%lld\n", 1ll * (m - ) * (n - ) - 1ll * tot);
for(int i = ; i <= ; i++) {
printf("%d\n", ans[i]);
}
return ;
}
AC代码
大意:
给定 n 个 a , m 个 b, k 个 c
求所组成的字符串的最小循环表示法的最大字典序。
解(结论):把这些一个一个的字符放入multiset,每次取字典序最大最小的合并放回去。
最后的即为所求。
#include <cstdio>
#include <set>
#include <iostream> using std::string; std::multiset<string> s; int main() {
int n;
scanf("%d", &n);
for(int i = ; i <= n; i++) {
s.insert("a");
}
scanf("%d", &n);
for(int i = ; i <= n; i++) {
s.insert("b");
}
scanf("%d", &n);
for(int i = ; i <= n; i++) {
s.insert("c");
}
while(s.size() > ) {
string a = *s.begin();
string b = *(--s.end());
s.erase(s.begin());
s.erase(--s.end());
s.insert((string)(a + b));
}
std::cout << *s.begin();
return ;
}
AC代码
不会...
DP题组的更多相关文章
- Codeforces Round #369 (Div. 2)---C - Coloring Trees (很妙的DP题)
题目链接 http://codeforces.com/contest/711/problem/C Description ZS the Coder and Chris the Baboon has a ...
- 4817 江哥的dp题d
4817 江哥的dp题d 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 已知1-N的排列P的LIS(最长上 ...
- 4809 江哥的dp题c
4809 江哥的dp题c 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 有两个数x,y,一开始x=1,y= ...
- 4816 江哥的dp题b
4816 江哥的dp题b 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 给出两个1-N的随机排列A,B.若 ...
- 4815 江哥的dp题a
4815 江哥的dp题a 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 给出一个长度为N的序列A(A1,A ...
- HDU 2577 How to Type(dp题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2577 解题报告:有一个长度在100以内的字符串,并且这个字符串只有大写和小写字母组成,现在要把这些字符 ...
- codevs4817 江哥的dp题d
4817 江哥的dp题d 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold [题目描述] Description 已知1-N的排列P的LIS(最长上升子序列)不超 ...
- 古韵之乞巧 题解 dp题
[noip模拟赛1]古韵之乞巧 描述 闺女求天女,更阑意未阑. 玉庭开粉席,罗袖捧金盘. 向月穿针易,临风整线难. 不知谁得巧,明旦试相看. ——祖咏<七夕> 女子乞巧,是七夕的重头戏 ...
- cf1061c 普通dp题
题解见https://blog.csdn.net/godleaf/article/details/84402128 这一类dp题是可以压缩掉一维空间的,本题枚举a1到an,枚举到ai时枚举ai的每个约 ...
随机推荐
- phpstorm显示页面不停的在indexing转圈中,并且文件名还一直在刷新
打开 File下的 Invalidate Caches / Restart...下的 Invalidate and Restart. 便可以了 ......
- python爬虫之pandas
一.简介: Python Data Analysis Library 或 pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.Pandas 纳入了大量库和一些标准的数据模 ...
- PHPStorm 配置命名空间
文件-设置-Directories 选中:application 点击顶部:Sources,右侧会出现 Source Floders 配置项 点击:p进行设置 输入app\
- Linux上面部署java项目
最近做项目迁移,费了很大周折.总算顺利迁移了.其实一直以为搞不懂单用tomcat是怎么发布项目的.但还是得硬着头皮做. 不过这个是在搭建测试服务器的时候弄的.开始我就直接把程序包丢tomcat里面也能 ...
- rediret 加/与不加/的区别
- iOS应用的性能调试
1.Static Analysis 使用之前先清理一下数据:product-->Clean 可能遇到的问题: a.发现工程中有多个“User-facing text should use loc ...
- 傻瓜式搭建私人网络硬盘——owncloud安装指南
百度云这个贱货天天删我资源,我已经忍无可忍了,于是想搭建一个owncloud来放我的里番.使用owncloud不仅安全,而且还可以在线播放,离线下载,功能相当强大. 然而·····网上查了一下,竟然无 ...
- 第二十天 模块 sys os os下path settings random shuit
一.sys模块 1.sys.argv 命令行参数List,第一个元素是程序本身路径 2.sys.exit(n) 退出程序,正常退出时exit(0) 3.sys.version 获取Pythonn解释程 ...
- BZOJ3230 相似子串 【后缀数组】
题目分析: 容易想到sa排好序之后,子串排名就是前面的子串减去height数组.所以正着做一遍,倒着做一遍就行了. 代码: #include<bits/stdc++.h> using na ...
- alter table,复制, 单表查询
修改表 语法:1. 修改表名 ALTER TABLE 表名 RENAME 新表名; 2. 增加字段 ALTER TABLE 表名 ...