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的每个约 ...
随机推荐
- mysql [assword expired
mysql 5.6 在使用Navicat在其他机器上进行远程登录数据库时 会出现 password expired ,需要重新设置一下密码. SET PASSWORD FOR 'root'@'%' = ...
- 转《service worker在移动端H5项目的应用》
1. PWA和Service Worker的关系 PWA (Progressive Web Apps) 不是一项技术,也不是一个框架,我们可以把她理解为一种模式,一种通过应用一些技术将 Web App ...
- mysql常用运算符
一.算数运算符 + 加法 - 减法 * 乘法 / 除法 % 返回余数 二.比较运算符 = 等于 <>或!= 不等于 <=> 等于(这里是安全的等于 例如: select nul ...
- Servlet学习的一些笔记
Servlet一点笔记 Servlet:Server Applet,全称Java Servlet,是运行在Web服务器或应用服务器上的程序,它是作为来自Web浏览器或其他HTTP客户端的请求和HTTP ...
- python易混易乱(1)
字典 基础操作 <1>keys my_dict = {"name":"zhangsan","age":18} res = my_ ...
- onbeforeunload事件两种写法及效果
在符合W3C标准的浏览器里,可以使用addEventListener方法来添加事件. 当不需要为一个事件添加多个处理函数的时候,可以简单的使用onXXX=function(){}的方式来添加事件处理函 ...
- 由 POST 400 错误拔出来的萝卜
缘起 前段时间遇到扫描问题,好不容易拿到了扫描出来的数据,结果调用接口时弹了个 400(Bad request) 给我,匆匆找了点资料修补上线后,忐忑的心也可以安分点.然后,顺着这个 400 的萝卜, ...
- border-color的深入理解
.className{ width:100px;height:100px; border:100px solid; border-color: red green blue orange; } 最终的 ...
- How to enable usb on vbox
Device-->Install Guest Addition Shared Folders Settings-->Advanced-->Shared Clipboard--> ...
- CSS3 flexbox 布局 ---- flex项目属性介绍
现在介绍用在flex项目上的css 属性,html结构还是用ul, li 结构,不过内容改成1,2,3, 样式的话,直接把给 ul 设display:flex 变成flex 容器,默认主轴的方向为水平 ...