F. Cards and Joy

F. Cards and Joy

题目大意:

给你n个人,每一个人恰好选k张牌。

第一行是 n 和 k

第二行有n*k个数,代表有n*k张牌,每张牌上的数字

第三行有n个数,代表第i个人喜欢的数字

第四行有k个数,代表有如果一个人可以拿到 i 张喜欢的牌,那么快乐值+h[i]

然后就是让你分配这些牌来找最大的欢乐值。

这个题目仔细想想就知道,因为这个h数组是递增的,所以我们就是要尽量把这个人喜欢的牌分配给她。

如果一种牌喜欢的人只有一个,那就把这个类型都给她,但是如果有很多人喜欢一样的牌,那就要合理分配使得x个人分配y张喜欢的牌使得欢乐值尽量大。

这个就是要dp了。

知道这个dp就很好d了。

dp[i][j] 表示i个人分配j张喜欢的牌,注意边界条件。

转移就是dp[i][j]=max(dp[i-1][j-x]+dp[1][x])

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <bitset>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1e5+10;
typedef long long ll;
ll dp[550][5005];
int c[maxn],f[maxn],h[maxn];
int vis[maxn],cnt[maxn];
pair<int,int>pii[maxn];
int main(){
    int n,k;
    scanf("%d%d",&n,&k);

    for(int i=1;i<=n*k;i++) scanf("%d",&c[i]),vis[c[i]]++;
    for(int i=1;i<=n;i++) scanf("%d",&f[i]),cnt[f[i]]++;
    for(int i=1;i<=k;i++) scanf("%d",&h[i]);

    sort(f+1,f+1+n);
    int len=unique(f+1,f+1+n)-f-1;

    int num=0,peo=0;

    for(int i=1;i<=len;i++){
        pii[i]=make_pair(vis[f[i]],cnt[f[i]]);
        num=max(vis[f[i]],num);
        peo=max(cnt[f[i]],peo);
    }
    for(int i=1;i<=min(num,k);i++) dp[1][i]=h[i];
    for(int i=2;i<=peo;i++){
        for(int j=1;j<=num;j++){
            for(int x=0;x<=min(j,k);x++){
                dp[i][j]=max(dp[i][j],dp[i-1][j-x]+dp[1][x]);
                // printf("dp[%d][%d]=%lld\n",i,j,dp[i][j]);
            }
        }
    }
    ll ans=0;
    for(int i=1;i<=len;i++){
        // printf("ww  %d %d\n",pii[i].second,pii[i].first);
        ans+=dp[pii[i].second][min(pii[i].first,pii[i].second*k)];
    }
    printf("%lld\n",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/EchoZQN/p/11631741.html

时间: 2024-08-02 19:19:40

F. Cards and Joy的相关文章

Codeforces Round #490 (Div. 3) F - Cards and Joy

F - Cards and Joy 思路:比较容易想到dp,直接dp感觉有点难,我们发现对于每一种数字要处理的情况都相同就是有 i 张牌 要给 j 个人分, 那么我们定义dp[ i ][ j ]表示 i 张牌给 j 个人分最大的价值可以得到dp方程如下: dp[ i ][ j ] = max(dp[ i - u ][ j - 1 ] + f[ u ] )   u <= k 暴力转移就好了. #include<bits/stdc++.h> #define LL long long #def

F.Cards with Numbers

链接:https://ac.nowcoder.com/acm/contest/908/F 题意: AFei has many cards. Each card has a number written on it. Now he wants to takes some out of his card and puts them in a box. And he wants to know whether the card with the number x was in the box. So

Codeforces 999F Cards and Joy 【dp】【性质】

1 #include<iostream> 2 #include<map> 3 #include<cstring> 4 using namespace std; 5 6 7 //只需要考虑每个number带来的贡献,不是任何人favorite number的数没有贡献 8 int a[5005],h[15],favorite[505];//favorite[i]为第i个player的favorite number 9 int vis[100005],ans; 10 map

Codeforces 999F Cards and Joy(二维DP)

题目链接:http://codeforces.com/problemset/problem/999/F 题目大意:有n个人,n*k张卡牌,每个人会发到k张卡牌,每个人都有一种喜欢的卡牌f[i],当一个人拥有x张喜欢的卡牌时会增加h[x]点愉悦值,求合理的发牌方式使得所有人的愉悦值之和最大,输出最大愉悦值.解题思路:设dp[x][y]表示当x个人拥有同一种喜欢的卡牌且该卡牌有y张时的最大愉悦值.则状态转移的根本取决于第x个人拥有几张喜欢的卡牌,所以得到状态转移方程:for (int i = 0;

过分过分进货价获国家

http://f.dangdang.com/group/24554/3491082/http://f.dangdang.com/group/24554/3491087/http://f.dangdang.com/group/24554/3491094/http://f.dangdang.com/group/24554/3491099/http://f.dangdang.com/group/24554/3491105/http://f.dangdang.com/group/24554/349111

我们找个地方看好戏

http://v.qq.com/page/f/y/4/m041433ssun.html http://v.qq.com/page/f/y/4/m041433ssun.html http://v.qq.com/page/f/y/4/m04143o3lhg.html http://v.qq.com/page/f/y/4/m04144675h3.html http://v.qq.com/page/f/y/4/m04144k1k1j.html http://v.qq.com/page/f/y/4/m04

Codeforces Round #376 (Div. 2) F. Video Cards

题解: 没想到是直接暴力求解..... 使用前缀和加速 代码: #include<bits/stdc++.h> #define ll long long using namespace std; const int maxn=1000100; int a[maxn],num[maxn]; int n,x; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&

高橋君とカード / Tak and Cards

高橋君とカード / Tak and Cards Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB Score : 300 points Problem Statement Tak has N cards. On the i-th (1≤i≤N) card is written an integer xi. He is selecting one or more cards from these N cards, so t

? D - 粉碎叛乱F - 其他起义

D - 粉碎叛乱 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1528 Description Adam and Eve play a card game using a regular deck of 52 cards. The rules are simple. The players sit on opposite sides