其他分享
首页 > 其他分享> > Cards and Joy CodeForces - 999F (贪心+set)

Cards and Joy CodeForces - 999F (贪心+set)

作者:互联网

There are nn players sitting at the card table. Each player has a favorite number. The favorite number of the jj-th player is fjfj.

There are k⋅nk⋅n cards on the table. Each card contains a single integer: the ii-th card contains number cici. Also, you are given a sequence h1,h2,…,hkh1,h2,…,hk. Its meaning will be explained below.

The players have to distribute all the cards in such a way that each of them will hold exactly kk cards. After all the cards are distributed, each player counts the number of cards he has that contains his favorite number. The joy level of a player equals htht if the player holds tt cards containing his favorite number. If a player gets no cards with his favorite number (i.e., t=0t=0), his joy level is 00.

Print the maximum possible total joy levels of the players after the cards are distributed. Note that the sequence h1,…,hkh1,…,hk is the same for all the players.

Input

The first line of input contains two integers nn and kk (1≤n≤500,1≤k≤101≤n≤500,1≤k≤10) — the number of players and the number of cards each player will get.

The second line contains k⋅nk⋅n integers c1,c2,…,ck⋅nc1,c2,…,ck⋅n (1≤ci≤1051≤ci≤105) — the numbers written on the cards.

The third line contains nn integers f1,f2,…,fnf1,f2,…,fn (1≤fj≤1051≤fj≤105) — the favorite numbers of the players.

The fourth line contains kk integers h1,h2,…,hkh1,h2,…,hk (1≤ht≤1051≤ht≤105), where htht is the joy level of a player if he gets exactly tt cards with his favorite number written on them. It is guaranteed that the condition ht−1<htht−1<ht holds for each t∈[2..k]t∈[2..k].

Output

Print one integer — the maximum possible total joy levels of the players among all possible card distributions.

Examples

Input
4 3
1 3 2 8 5 5 8 2 2 8 5 2
1 2 2 5
2 6 7
Output
21
Input
3 3
9 9 9 9 9 9 9 9 9
1 2 3
1 2 3
Output
0

Note

In the first example, one possible optimal card distribution is the following:

Thus, the answer is 2+6+6+7=212+6+6+7=21.

In the second example, no player can get a card with his favorite number. Thus, the answer is 00.

 

题意:

给你N个数的数组,还有一个数m,m一定是n的因子,。

现在你可以改变数组中的每一个数,使之n个数对m取模后的结果值的数量严格为n/m

让一个数+1的成本是1,问最小的改变成本是多少?

 

思路:

巧妙的运用了set的功能和贪心的思想。

 

先把0~m-1的所有数加入到set中,

然后扫一遍数组,对于每一个a[i],

x=a[i]%m

然后我们就要从set中找到那个让a[i]增加值最小的那个取模后的数,

如果x比set中所有的数大,那么我们必须让它加一点数然后变成%m后是set中最小数才可以成本最低。

否则我们需要用到set中的一个函数lower_bound()

这个函数的作用想必都知道,就说在set中找到第一个大于等于x的数。

注意函数返回的是一个set的迭代器,*iterator 才是取值。

然后把a[i]进行改变,最后输出答案。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll n,m;
ll a[maxn];
ll b[maxn];
ll c[maxn];
int main()
{
    //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
    //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
    gbtb;
    cin>>n>>m;
    ll num=n/m;
    repd(i,1,n)
    {
        cin>>a[i];
    }
    set<int> st;
    repd(i,0,m-1)
    {
        st.insert(i);
    }
    ll x;
    ll y;
    ll ans=0ll;
    repd(i,1,n)
    {
        x=a[i]%m;
        if(x>(*st.rbegin()))
        {
            y=*st.begin();
        }else
        {
            y=*st.lower_bound(x);
        }
        b[y]++;
        if(b[y]==num)
        {
            st.erase(y);
        }
        a[i]+=((y-x)+m)%m;
        ans+=((y-x)+m)%m;
    }
    cout<<ans<<endl;
    repd(i,1,n)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}

 

标签:set,Joy,CodeForces,number,player,cards,include,define
来源: https://www.cnblogs.com/qieqiemin/p/10720195.html