Codeforces Round #490 (Div. 3) :F. Cards and Joy(组合背包)
题目连接:http://codeforces.com/contest/999/problem/F
解题心得:
- 题意说的很复杂,就是n个人玩游戏,每个人可以得到k张卡片,每个卡片上有一个数字,每个人有一个喜欢的数字,每一个玩游戏的人如果有不同数量自己喜欢的卡片就有不同的欢乐值。问怎样分配使欢乐值最大。
- 就是一个组合背包的问题,dp[i][j]代表有i个人,j张喜欢的卡片,得到的总欢乐值最大是多少。在转移的过程中只需要枚举第i个人有几张自己最喜欢的卡片就可以了。
- 转移方程式dp[i][j] = max(dp[i-1][j-k]+hz[k], dp[i][j])
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = ;
const int maxm = ; ll dp[maxn][maxm],n,k,ht[],card[maxm],favorite[maxn],ht1[];
map <int,int> maps;
map <int,int> cnt_card; void init() {
scanf("%lld%lld",&n,&k);
for(int i=;i<=k*n;i++) {
scanf("%lld", &card[i]);
cnt_card[card[i]]++;
}
for(int i=;i<=n;i++) {
scanf("%lld", &favorite[i]);
maps[favorite[i]]++;
}
for(int i=;i<=k;i++)
scanf("%lld",&ht[i]);
} void DP() {
for(int i=; i<=n; i++) {
for (int z = ; z <= k; z++) {
for (int j = n * k; j >= ; j--) {
if(z > j)
break;
dp[i][j] = max(dp[i][j], dp[i - ][j - z] + ht[z]);
}
}
}
} int main() {
init();
DP();
map <int,int> :: iterator iter;
ll ans = ;
for(iter=maps.begin();iter!=maps.end();iter++) {
int num = iter->first;
int cnt = iter->second;
ll cnt_fav;
if(cnt_card[num] > cnt*k){
cnt_fav = cnt*k;
} else {
cnt_fav = cnt_card[num];
}
ans += dp[cnt][cnt_fav];
}
printf("%lld",ans);
return ;
}
Codeforces Round #490 (Div. 3) :F. Cards and Joy(组合背包)的更多相关文章
- Codeforces Round #490 (Div. 3) F - Cards and Joy
F - Cards and Joy 思路:比较容易想到dp,直接dp感觉有点难,我们发现对于每一种数字要处理的情况都相同就是有 i 张牌 要给 j 个人分, 那么我们定义dp[ i ][ j ]表示 ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #490 (Div. 3)
感觉现在\(div3\)的题目也不错啊? 或许是我变辣鸡了吧....... 代码戳这里 A. Mishka and Contes 从两边去掉所有\(≤k\)的数,统计剩余个数即可 B. Reversi ...
- Codeforces Round #376 (Div. 2) F. Video Cards —— 前缀和 & 后缀和
题目链接:http://codeforces.com/contest/731/problem/F F. Video Cards time limit per test 1 second memory ...
- Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)
题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...
- Codeforces Round #376 (Div. 2) F. Video Cards 数学 & 暴力
http://codeforces.com/contest/731/problem/F 注意到一个事实,如果你要找一段区间中(从小到大的),有多少个数是能整除左端点L的,就是[L, R]这样.那么,很 ...
随机推荐
- 基于NFS实现多WEB服务器负载均衡
实现环境: 实现原理: 共四台服务器 A,B,C,D 服务器A (CentOS 6.7): IP地址: 192.168.3.67 角色: DNS服务 说明: 为两台web服务器做域名轮询 服务器B,C ...
- EJB 3.1 @Startup @Singleton sequence
The annotation javax.ejb.Startup (@Startup) is used to mark an EJB so to make the EJB can be brought ...
- Intellij IDEA 代码提示忽略大小写
1.0 File >>Settings 2.0 Editor >> General >> Code Completion 如下图 选择 None
- HTML5 classList API
Having thrust myself into the world of JavaScript and JavaScript Libraries, I've often wondered: Whe ...
- 记录一下xcode9 添加文件夹的顺序
右击->Add Files to YourProject->弹出文件对话框,点击Options->选择:Copy Items if needed和Create Groups 要选择O ...
- 【luogu P4017 最大食物链计数】 题解
题目链接:https://www.luogu.org/problemnew/show/P4017 DAG + DP #include <queue> #include <cstdio ...
- Linq不分组求多列的和
我们需要写Linq查询语句,使用let来创建一个新的变量 Let 关键字 “let”关键字在查询语法中很有用.它会投影一个新的范围变量,允许重新使用表达式并使查询更具可读性. 例如: 这里需要写,两遍 ...
- 课时18.h标签和p标签以及hr标签(掌握)
如何在webstorm中利用快捷键创建一个新的html的文件? 同时按下键盘上的ctrl+alt+insert(windows) 同时按下键盘上的ctrl+alt+n(os) h标签系列(header ...
- LeetCode 中级 -二叉树的层次遍历(102)
题目描述: 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如:给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 ...
- 02.将python3作为centos7的默认python命令
博客为日常工作学习积累总结: 由于个人兴趣爱好对python有了解: 1.安装Python3: 参考博客:https://zhuanlan.zhihu.com/p/47868341 安装依赖包: yu ...