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 ...
随机推荐
- spring的DI.IoC是什么
最近要搞spring的单元测试,不得已啊啊啊啊啊啊啊啊啊又要开始搞spring…… 日目晶…… 搞这几个概念,先甩一部分代码: UserDao 接口 package com.itheima.ioc; ...
- CentOS7_防火墙
1.查看防火墙状态 firewall-cmd --state 2.启动防火墙 systemctl start firewalld.service 3.关闭防火墙 systemctl stop fire ...
- .NET框架 - NETFramework + API + EF(DBFirst) + MYSQL
.NET框架 - NETFramework + MVC+ EF(DBFirst) + MYSQL 1. 安装3个MYSQL插件 ①mysql-for-visualstudio-1.2.8 vs的 ...
- EF的默认映射以及如何使用Data Annotations和Fluent API配置数据库的映射
I.EF的默认映射 上节我们创建项目,通过定义实体类就可以自动生成数据库,并且EF帮我们自动设置了数据库的主键.外键以及表名和字段的类型等,这就是EF的默认映射.具体分为: 数据库映射:Code Fi ...
- [转载] win10进行端口转发
1.添加端口转发netsh interface portproxy add v4tov4 listenport=4000 listenaddress=127.0.0.1 connectport=400 ...
- Kafka(二)设计原理
1.持久性 kafka使用文件存储消息,这就直接决定kafka在性能上严重依赖文件系统的本身特性.且无论任何OS下,对文件系统本身的优化几乎没有可能.因为kafka是对日志进行append操作,因此磁 ...
- 开发一个项目之css
background属性 5+3 image,color,position,repeat,attachment size: 保持纵横比 contain 再发大就cover了 clip:背景绘制区域 b ...
- Ajax数据爬取
Ajax的基本原理 以菜鸟教程的代码为例: XMLHTTPRequest对象是JS对Ajax的底层实现: var xmlhttp; if (window.XMLHttpRequest) { // IE ...
- 2018-2019-2 20165231 王杨鸿永 Exp6 信息搜集与漏洞扫描
实践目标 掌握信息搜集的最基础技能与常用工具的使用方法. 2.实践内容 (1)各种搜索技巧的应用 (2)DNS IP注册信息的查询 (3)基本的扫描技术:主机发现.端口扫描.OS及服务版本探测.具体服 ...
- 20175315 实验二《Java面向对象程序设计》实验报告
20175315 实验二<Java面向对象程序设计>实验报告 一.实验内容及步骤 1.初步掌握单元测试和TDD 单元测试 任务一:三种代码 用程序解决问题时,要学会写以下三种代码: 伪代码 ...