CodeForces 1070J Streets and Avenues in Berhattan 性质+动态规划
题目大意:
你有$k$个数,分为$26$种
对于每个数,你可以选择选进$A$集合或者$B$集合或者不选
要求$A$集合中必须有$n$个数,$B$集合中必须有$m$个数
记第$i$种数在$A$集合中的个数为$a_i$,$B$中个数为$b_i$
试最小代价$\sum a_i * b_i$
$k \leqslant 200000$,$n, m \leqslant 30000$
首先,我们打表得出一个结论,代价一定只由一种数得出
考虑证明:
我们不妨设代价由$A$得出,且集合$S_A$和$S_B$中分别有$i$个$A$和$a - i$个$A$
那么,如果我们尝试用$B$来替换$A$,不妨设从$B$中抽了$j$个$B$扔进$A$中,且一共有$b$个$B$
那么贡献差为$i * (a - i) - ((i - j) * (a - i + j) + j * (b - j))(0 \leq j \leq min(i, b))$
化简后,为$2j^2 - j(2i - a+ b)$
这是一个开口向上的,以$j$为自变量的二次函数
最大值一定在端点取到,也就是$j = 0$或者$j = i$或者$j = b$取到
这三种情况对应着代价由$A$得出或者由$B$得出
我们可以枚举在中间的种类是哪一个
之后再枚举放在$A$中的数能取多少个
相应地我们可以知道最多可以放在$B$中多少个
可以通过背包来实现上述问题
由于实现不优,复杂度为$O(26^2 * (n+ m) + 26 * k)$
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
namespace remoon {
#define ri register int
#define tpr template <typename ra>
#define rep(iu, st, ed) for(ri iu = st; iu <= ed; iu ++)
#define drep(iu, ed, st) for(ri iu = ed; iu >= st; iu --)
#define gc getchar
inline int read() {
int p = , w = ; char c = gc();
while(c > '' || c < '') { if(c == '-') w = -; c = gc(); }
while(c >= '' && c <= '') p = p * + c - '', c = gc();
return p * w;
}
int wr[], rw;
#define pc(iw) putchar(iw)
tpr inline void write(ra o, char c = '\n') {
if(!o) pc('');
if(o < ) o = -o, pc('-');
while(o) wr[++ rw] = o % , o /= ;
while(rw) pc(wr[rw --] + '');
pc(c);
}
}
using namespace std;
using namespace remoon;
#define sid 30050
#define kid 200050 char s[kid];
bool f[kid];
int n, m, k, t, cnt[]; inline int judge() {
f[] = ;
memset(f, , sizeof(f));
rep(i, , ) drep(j, k, cnt[i])
f[j] |= f[j - cnt[i]];
rep(i, n, k - m)
if(f[i]) { puts(""); return ; }
return ;
} inline void solve() {
int ans = 1e9;
rep(i, , ) {
memset(f, , (n + m) << );
f[] = ;
rep(j, , ) if(i != j)
drep(v, n + m, cnt[j]) f[v] |= f[v - cnt[j]]; rep(j, , n + m)
if(f[j]) {
int l = max(n - j, );
int r = max(m - (k - cnt[i] - j), );
if(l + r <= cnt[i]) ans = min(ans, l * r);
}
}
write(ans);
} int main() {
t = read();
while(t --) {
n = read(); m = read(); k = read();
scanf("%s", s + );
int sn = strlen(s + );
memset(cnt, , sizeof(cnt));
rep(i, , sn) ++ cnt[s[i] - 'A'];
if(judge()) continue;
solve();
}
return ;
}
CodeForces 1070J Streets and Avenues in Berhattan 性质+动态规划的更多相关文章
- Codeforces 1070J Streets and Avenues in Berhattan dp
Streets and Avenues in Berhattan 我们首先能发现在最优情况下最多只有一种颜色会分别在行和列, 因为你把式子写出来是个二次函数, 在两端取极值. 然后我们就枚举哪个颜色会 ...
- CF 1070J Streets and Avenues in Berhattan
DP的数组f其实开得不够大,应该开200000,但是它在cf上就是过了... 题意是把一堆字母分别分配到行和列. 分析一下,答案实际上只和n行中和m列中每种字母分配的个数有关.而且答案只和" ...
- 2018-2019 ICPC, NEERC J. Streets and Avenues in Berhattan(DP)
题目链接:https://codeforc.es/contest/1070/problem/J 题意:给出一个长度为 k 的字符串,选出 n 个和 m 个不同位置的字符构成两个字符串,使得两个字符串相 ...
- codeforces 447E or 446C 线段树 + fib性质或二次剩余性质
CF446C题意: 给你一个数列\(a_i\),有两种操作:区间求和:\(\sum_{i=l}^{r}(a[i]+=fib[i-l+1])\).\(fib\)是斐波那契数列. 思路 (一) codef ...
- Codeforces 1383C - String Transformation 2(找性质+状压 dp)
Codeforces 题面传送门 & 洛谷题面传送门 神奇的强迫症效应,一场只要 AC 了 A.B.D.E.F,就一定会把 C 补掉( 感觉这个 C 难度比 D 难度高啊-- 首先考虑对问题进 ...
- Codeforces 1067E - Random Forest Rank(找性质+树形 dp)
Codeforces 题面传送门 & 洛谷题面传送门 一道不知道能不能算上自己 AC 的 D1E(?) 挺有意思的结论题,结论倒是自己猜出来了,可根本不会证( 开始搬运题解 ing: 碰到这样 ...
- Codeforces 809C - Find a car(找性质)
Codeforces 题目传送门 & 洛谷题目传送门 首先拿到这类题第一步肯定要分析题目给出的矩阵有什么性质.稍微打个表即可发现题目要求的矩形是一个分形.形式化地说,该矩形可以通过以下方式生成 ...
- Codeforces Round #382 (Div. 2)C. Tennis Championship 动态规划
C. Tennis Championship 题目链接 http://codeforces.com/contest/735/problem/C 题面 Famous Brazil city Rio de ...
- Codeforces 822D My pretty girl Noora - 线性筛 - 动态规划
In Pavlopolis University where Noora studies it was decided to hold beauty contest "Miss Pavlop ...
随机推荐
- D - Doing Homework HDU - 1074 (状压dp)
题目链接:https://cn.vjudge.net/contest/68966#problem/D 具体思路:我们可以把每个情况都枚举出来,然后用递归的形式求出最终的情况. 比如说 我们要求 10 ...
- 2018ICPC青岛赛区J题
题目链接:http://acm.zju.edu.cn/onlinejudge/showRuns.do?contestId=1 这题真的坑,为什么要买0本书的时候,书架里面刚好有价格为0的时候输出&qu ...
- 【bzoj题解】1012 最大数
题目描述 现在请求你维护一个数列,要求提供以下两种操作:1.查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度.2.插入操作.语法:A ...
- Python Challenge 第 4 关攻略:linkedlist
代码 import requests url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing={}" ...
- linux创建新用户
服务器只用root账号会有风险,最好是每个人使用一个账号. 1. 添加用户名 adduser linuxidc 2. 给这个用户名设置新密码 passwd linuxidc 3.授权 个人用户的权限只 ...
- 查找Python包的依赖包(语句)
Window 10家庭中文版,Python 3.6.4, 今天看完了urllib3的官文(官方文档),因为没有具体使用过,所以,仍然是一知半解,但是,突然想知道 urllib3以及前面学习过的requ ...
- 简易博客[ html + css ] 练习
1. 前言 通过使用 html + css 编写一个简易的博客作为入门练习 2. 代码及实现 2.1 目录结构 2.2 代码部分 <!DOCTYPE html> <html lang ...
- 原生JS实现AJAX、JSONP及DOM加载完成事件
一.JS原生Ajax ajax:一种请求数据的方式,不需要刷新整个页面:ajax的技术核心是 XMLHttpRequest 对象:ajax 请求过程:创建 XMLHttpRequest 对象.连接服务 ...
- linux文件处理
取中间的行数作为train.txt sed -n '1000000,170910580p' train.txt > trainv1.txt 取前面的行数作为dev.txt head -10000 ...
- P2471 [SCOI2007]降雨量
Description 我们常常会说这样的话:"X年是自Y年以来降雨量最多的".它的含义是X年的降雨量不超过Y年,且对于任意Y<Z<X,Z年的降雨量严格小于X年.例如2 ...