Codeforces 1070J Streets and Avenues in Berhattan dp
Streets and Avenues in Berhattan
我们首先能发现在最优情况下最多只有一种颜色会分别在行和列, 因为你把式子写出来是个二次函数, 在两端取极值。
然后我们就枚举哪个颜色会分别在行和列。 然后枚举这种颜色在行的个数, 再求出需要在列放的最少的这种颜色的个数。
这个我们可以用dp来check, dp[ i ] 表示 完整地加入若干种颜色能否恰好为 i , 然后再把dp[ i ]转成, 能组成大于等于 i 的最小值。
- #include<bits/stdc++.h>
- #define LL long long
- #define LD long double
- #define ull unsigned long long
- #define fi first
- #define se second
- #define mk make_pair
- #define PLL pair<LL, LL>
- #define PLI pair<LL, int>
- #define PII pair<int, int>
- #define SZ(x) ((int)x.size())
- #define ALL(x) (x).begin(), (x).end()
- #define fio ios::sync_with_stdio(false); cin.tie(0);
- using namespace std;
- const int N = 2e5 + ;
- const int inf = 0x3f3f3f3f;
- const LL INF = 0x3f3f3f3f3f3f3f3f;
- const int mod = 1e9 + ;
- const double eps = 1e-;
- const double PI = acos(-);
- template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
- template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < ) a += mod;}
- template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
- template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}
- int n, m, k, dp[N];
- int c[];
- char s[N];
- void getDp(int ban) {
- for(int i = ; i <= k; i++) dp[i] = ;
- dp[] = ;
- for(int i = ; i < ; i++) {
- if(i == ban) continue;
- for(int j = k; j >= ; j--) {
- if(dp[j]) dp[j + c[i]] |= dp[j];
- }
- }
- for(int i = k; i >= ; i--) {
- if(dp[i]) dp[i] = i;
- else dp[i] = dp[i + ];
- }
- }
- int main() {
- int T; scanf("%d", &T);
- while(T--) {
- memset(c, , sizeof(c));
- scanf("%d%d%d", &n, &m, &k);
- scanf("%s", s);
- for(int i = ; s[i]; i++) c[s[i] - 'A']++;
- getDp(-);
- if(k - dp[n] >= m) {
- puts("");
- } else {
- LL ans = INF;
- for(int i = ; i < ; i++) {
- getDp(i);
- for(int j = ; j <= c[i] && j <= n; j++) {
- if(n - j > k - c[i]) continue;
- int res = (k - c[i]) - dp[n - j];
- if(m - res + j <= c[i])
- chkmin(ans, 1LL * (m - res) * j);
- }
- }
- printf("%lld\n", ans);
- }
- }
- return ;
- }
- /*
- */
Codeforces 1070J Streets and Avenues in Berhattan dp的更多相关文章
- CodeForces 1070J Streets and Avenues in Berhattan 性质+动态规划
题目大意: 你有$k$个数,分为$26$种 对于每个数,你可以选择选进$A$集合或者$B$集合或者不选 要求$A$集合中必须有$n$个数,$B$集合中必须有$m$个数 记第$i$种数在$A$集合中的个 ...
- 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 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- [CodeForces - 1272D] Remove One Element 【线性dp】
[CodeForces - 1272D] Remove One Element [线性dp] 标签:题解 codeforces题解 dp 线性dp 题目描述 Time limit 2000 ms Me ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- codeforces 425C Sereja and Two Sequences(DP)
题意读了好久才读懂....不知道怎么翻译好~~请自便~~~ http://codeforces.com/problemset/problem/425/C 看懂之后纠结好久...不会做...仍然是看题解 ...
- Codeforces Round #131 (Div. 1) B. Numbers dp
题目链接: http://codeforces.com/problemset/problem/213/B B. Numbers time limit per test 2 secondsmemory ...
- Codeforces Round #131 (Div. 2) B. Hometask dp
题目链接: http://codeforces.com/problemset/problem/214/B Hometask time limit per test:2 secondsmemory li ...
随机推荐
- Redis-Cluster操作命令大全
今天整理下redis-cluster操作命令 一.Cluster操作命令 CLUSTER INFO 打印集群的信息 CLUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相 ...
- thrift使用
一.什么是thrift Thrift是一种接口描述语言和二进制通讯协议,它被用来定义和创建跨语言的服务.它被当作一个远程过程调用(RPC)框架来使用,是由FaceBook为“大规模跨语言服务开发”而开 ...
- Apache Shiro Java反序列化漏洞分析
1. 前言 最近工作上刚好碰到了这个漏洞,当时的漏洞环境是: shiro-core 1.2.4 commons-beanutils 1.9.1 最终利用ysoserial的CommonsBeanuti ...
- SVN状态说明
svn status查看工作副本中目录与文件的状态 命令格式:svn status [PATH](简写:svn st) 打印工作拷贝中文件和目录的状态. svn st|grep ^状态(获得某状态文件 ...
- Airflow Comman Line 测试
官网文档:https://incubator-airflow.readthedocs.io/en/latest/cli.html clear (1)clear 指定日期某一个dag下的任务,任务名可以 ...
- hive group by聚合函数增强
1.grouping sets grouping sets子句都可以根据UNION连接的多个GROUP BY查询进行逻辑表示 SELECT a,b,SUM(c)FROM tab1 GROUP BY a ...
- java程序设计习题总结
---恢复内容开始--- main()方法的参数名可以改变:main()方法的参数个数不可以改变. 当一个程序没有main()方法是,是可以编译通过的,但是不能给运行,因为找不到一个主函数入口. 标识 ...
- [系统集成] RT(Request Tracker)执行自定义脚本及发送微信、短信的实现方法
RT(Request Tracker)是一个基于 Perl 语言的开源状态跟踪和工作流系统,支持审批.权限管理等功能,中文化支持的也不错,可以作为企业的流程审批系统使用.可惜的是,该系统在国内使用的不 ...
- 第十六节,OpenCV(学习五)边缘检测
边缘检测 边缘检测的目的是标识数字图像中亮度变化明显的点,边缘检测是特征提取的重要领域. 1.检测方法 边缘检测的方法大致分为两类:基于搜索和基于零交叉 基于搜索的边缘检测方法首先计算边缘强度,通常用 ...
- 本地项目文件夹上传至个人Github
安装Git 之后到Git官网,点击Download下载,打开安装包一路按Next一切默认直至安装结束. 找到任意一个文件夹,点击鼠标右键后若出现下图的 Git Gui Here 和 Git Bash ...