2416. Berry Picking

2416. Berry Picking

Title description

Bessie and her sister Elsie are picking berries in Farmer John's berry garden. There are N berry trees in Farmer John's berry garden (1≤N≤1000); there are Bi berries on tree i (1≤Bi≤1000). Bessie has K baskets (1≤K≤1000, K is an even number). Each basket can hold any number of berries picked from the same tree, but it cannot contain berries from different trees because their tastes may be different. It is not necessary to hold berries in the basket.
Bessie wants to maximize the number of berries she gets. However, Farmer John wants Bessie to share with her sister, so Bessie must give Elsie K / 2 baskets with more berries. This means that Elsie is likely to get more berries than Bessie in the end, which is very unfair, but this is often the case between sisters.
Help Bessie find the maximum number of berries she can get.

Input

The first line of input contains integers N and K separated by spaces.
The second line contains N space-separated integers B1, B2, ..., BN.

Output

A line is output containing the requested answer.

Sample input

5 4
3 6 8 4 2

Sample output

8

Data range limitation

Test points 1-3 satisfy K≤10.
Test points 4-10 have no additional restrictions.

prompt

If Bessie holds 6 berries of tree 2 in one basket,
4 berries of tree 3 in two baskets, and 4 berries of
tree 4 in one basket,
then she can get two baskets with 4 berries each. , A total of 8 berries.

The idea of ​​the topic:
In the best case, the total number of berries obtained by Bessie is at most m * (k / 2) (m means the youngest sister gets the smallest basket (theoretically m can be from 1 to maxn)).
Therefore, we assume that i (i = 1 ~ maxn) berries are taken from each tree, and at most lz basket berries can be obtained. (Each basket is i). Discuss in three situations:

  1. If lz <(k / 2) she wo n’t take so many berries per basket (i gets smaller, she will get more, think slowly, you will understand)
  2. If lz> = k, this is best understood, this is the best case, Bessie will get i * (k / 2) berries;
  3. (K / 2) <lz <k, in this case, Bessie will first take the lz- (k / 2) basket, and then try to take the basket close to i until it is enough;
    Note: we enumerate i, meet the conditions The solution is not necessarily the optimal solution, you need to compete in the ring, you know;
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
using namespace std;
const int MAX=2147483647;
const int N=1e3+5;
int n,k,maxn,ans,mod,f[N];
bool cmp(int x, int y) {return x%mod>y%mod;}
void input()
{
	cin>>n>>k;
	for (int i=1;i<=n;i++) 
	{
        cin>>f[i];
        maxn=max(maxn,f[i]);
    }
}
void work()
{
	for (int i=1;i<=maxn;i++)
	{
    	int lz=0;         //lz表示每篮最少放i个浆果,最多可以装多少篮; 
    	for (int j=1;j<=n;j++)
        	lz+=f[j]/i;
        if(lz<(k/2))   break;
        if(lz>=k) 
		{
            ans=max(ans,i*(k/2));
            continue;
    	}
		//(k/2)<lz<k的情况,先取(lz-(k/2))*i,然后对每棵树按取模i的情况降序排序;
		//按顺序取够剩下的篮数(贪心:每篮都是不够i颗浆果的,但最接近i颗的篮子优先取;) 
		mod=i;
        sort(f+1,f+n+1,cmp);
        int temp=(lz-(k/2))*i;
        for(int j=1;j<=n&&j+lz<=k;j++)
            temp+=f[j]%i;  //注意这里是顺序取每棵树取模剩下最多浆果的那一篮,取够为止; 
        ans=max(ans,temp); 
	}
}
int main()
{
	//fre(berries);
	input();
    work();
    cout<<ans;
	return 0;
}

Published 130 original articles · Like 93 · Visits 6798

Guess you like

Origin blog.csdn.net/bigwinner888/article/details/105460891