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的更多相关文章

  1. CodeForces 1070J Streets and Avenues in Berhattan 性质+动态规划

    题目大意: 你有$k$个数,分为$26$种 对于每个数,你可以选择选进$A$集合或者$B$集合或者不选 要求$A$集合中必须有$n$个数,$B$集合中必须有$m$个数 记第$i$种数在$A$集合中的个 ...

  2. CF 1070J Streets and Avenues in Berhattan

    DP的数组f其实开得不够大,应该开200000,但是它在cf上就是过了... 题意是把一堆字母分别分配到行和列. 分析一下,答案实际上只和n行中和m列中每种字母分配的个数有关.而且答案只和" ...

  3. 2018-2019 ICPC, NEERC J. Streets and Avenues in Berhattan(DP)

    题目链接:https://codeforc.es/contest/1070/problem/J 题意:给出一个长度为 k 的字符串,选出 n 个和 m 个不同位置的字符构成两个字符串,使得两个字符串相 ...

  4. Codeforces 219D. Choosing Capital for Treeland (树dp)

    题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

  5. [CodeForces - 1272D] Remove One Element 【线性dp】

    [CodeForces - 1272D] Remove One Element [线性dp] 标签:题解 codeforces题解 dp 线性dp 题目描述 Time limit 2000 ms Me ...

  6. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  7. codeforces 425C Sereja and Two Sequences(DP)

    题意读了好久才读懂....不知道怎么翻译好~~请自便~~~ http://codeforces.com/problemset/problem/425/C 看懂之后纠结好久...不会做...仍然是看题解 ...

  8. Codeforces Round #131 (Div. 1) B. Numbers dp

    题目链接: http://codeforces.com/problemset/problem/213/B B. Numbers time limit per test 2 secondsmemory ...

  9. Codeforces Round #131 (Div. 2) B. Hometask dp

    题目链接: http://codeforces.com/problemset/problem/214/B Hometask time limit per test:2 secondsmemory li ...

随机推荐

  1. Java第五周学习总结

    学号 2016-2017-2 <Java程序设计>第X周学习总结 教材学习内容总结 1.接口 (1)使用关键字interface来定义一个接口,接口分为接口声明和接口体,例如 interf ...

  2. Gradle里面两个 依赖管理插件,可以不用关心 具体jar版本号

    引用:https://spring.io/blog/2015/02/23/better-dependency-management-for-gradle Using the plugin with S ...

  3. Java基础--面向对象编程3(继承)

    1.继承的作用 为了提取两个类中公共的代码,可以使用继承抽取重复性的代码到一个公共类中. 这个公共的类称为父类(super class),继承于父类的类称为子类(sub class). 2.java继 ...

  4. c++ boost库配置

    1.官方下载地址 https://www.boost.org/ 2.下载解压 3.配置VS 4.配置目录

  5. 【转】Nginx 学习笔记(十一)nginx下安装配置naxsi waf防火墙(附完整编译、配置)

    原文地址:http://f2ex.cn/nginx-installed-configuration-naxsi-waf/ Naxsi 是第三方 nginx 模块 ,它和 Modsecurity 都是开 ...

  6. 技术栈(technology stack)

    technology stack 技术栈: 产品实现上依赖的软件基础组件, 包括 1. 系统 2. 中间件 3. 数据库 4. 应用软件 5. 开发语言 6. 框架 https://en.wikipe ...

  7. jmeter中的参数化

    1.那些场景需要参数化? 1.登陆认证信息 2.一些和时间相关的,违反时间约束的[时间点和当前时间不一致的情况等等] 3.一些受其他字段约束的[例如字段的一些限制条件] 4.一些来自于其他数据源[例如 ...

  8. python3 练手实例1 计算三角形周长和面积

    def j(): a,b,c=map(float,input('请输入三角形三条边的长度,用空格隔开:').split()) if a>0 and b>0 and c>0 and a ...

  9. python基本面试题

    https://www.cnblogs.com/changwentao/p/9432166.html

  10. C# 读取 Excel 最全最稳定的方式

    采用 NPOI 和 Epplus 方式读取Excel,因为之前测试NPOI的时候发现对于 .xlsx 格式的文件读取不是很友好,所以才结合了两个. 废话少说,直接上代码: public static ...