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. 思维导图读PMbok第6版 - 项目整合管理(21张全讲)

    “ 3个月,800多页书,一大堆工作,复习时间不够呀?老师用思维导图解析PMP,思维导图解析PMP梳理PMbok第6版逻辑结构,帮你您全局掌握PMP知识,重点掌握PMbok难点.快速记忆PMP知识,思 ...

  2. 使用基本MVC2模式创建新闻网站

    MVC简介 所谓MVC,即Model-View-Controller. (1)Model层:Model指模型部分,一般在应用中Model层包括业务处理层和数据访问层.数据访问层主要是对数据库的一些操作 ...

  3. jenkins+ant+jmeter接口测试

    <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl=" ...

  4. SpringMVC生命周期,SpringMVC运行流流程

    SpringMVC详细运行流程图 SpringMVC运行原理 1. 客户端请求提交到DispatcherServlet2. 由DispatcherServlet控制器查询一个或多个HandlerMap ...

  5. java eclipse中使用wsdl生成soap 的客户端代码

    项目右键--> new --> other finish完成

  6. TCP和UDP的优缺点及区别

    1.TCP是什么? TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议. TCP的优点: 可靠,稳定 TCP的可靠体 ...

  7. Java原子类中CAS的底层实现

    Java原子类中CAS的底层实现 从Java到c++到汇编, 深入讲解cas的底层原理. 介绍原理前, 先来一个Demo 以AtomicBoolean类为例.先来一个调用cas的demo. 主线程在f ...

  8. 使用lombok 注解Java类

    环境信息: IDEA  2016.2.4 Maven 3 JDK 1.8 Maven工程配置: <properties> <lombok.version>1.16.16< ...

  9. redis---------AOF文件异常导致的redis无法载入

    AOF损坏时的对策1.若在写AOF文件时Server崩溃则可能导致AOF文件损坏而不能被Redis载入.可通过如下步骤修复: 创建一个AOF文件的备份: cp appendonly.aof appen ...

  10. P3203 [HNOI2010]弹飞绵羊

    LCT裸题,之后填坑打一下 分块做法:每个点存几次出块以及出块的位置,问的时候直接暴力跳就vans了 首先思考最普通的模拟,发现可以O(n)路径压缩,O(1)的查询,但是需要修改就变成了O(n^2)的 ...