The Feast and the Bus (暴力)

博客围绕JebTrains员工庆祝活动租车问题展开。有n名员工和k个团队,员工分属不同团队。租车可搭载一或两个完整团队,不能拆分团队。要计算满足条件下,将所有员工送到活动场地的最小租车费用,还给出了输入输出示例。

Employees of JebTrains are on their way to celebrate the 256-th day of the year! There are n employees and k teams in JebTrains. Each employee is a member of some (exactly one) team. All teams are numbered from 1 to k. You are given an array of numbers t1,t2,…,tn where ti is the i-th employee’s team number.

JebTrains is going to rent a single bus to get employees to the feast. The bus will take one or more rides. A bus can pick up an entire team or two entire teams. If three or more teams take a ride together they may start a new project which is considered unacceptable. It’s prohibited to split a team, so all members of a team should take the same ride.

It is possible to rent a bus of any capacity s. Such a bus can take up to s people on a single ride. The total cost of the rent is equal to s⋅r burles where r is the number of rides. Note that it’s impossible to rent two or more buses.

Help JebTrains to calculate the minimum cost of the rent, required to get all employees to the feast, fulfilling all the conditions above.

Input
The first line contains two integers n and k (1≤n≤5⋅105,1≤k≤8000) — the number of employees and the number of teams in JebTrains. The second line contains a sequence of integers t1,t2,…,tn, where ti (1≤ti≤k) is the i-th employee’s team number. Every team contains at least one employee.

Output
Print the minimum cost of the rent.

Examples
Input
6 3
3 1 2 3 2 3
Output
6
Input
10 1
1 1 1 1 1 1 1 1 1 1
Output
10
Input
12 4
1 2 3 1 2 3 4 1 2 1 2 1
Output
12

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int s[1000005];
LL n, m, a, ans = 1e18, r, x;
int main() {
    ios::sync_with_stdio(false);
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> a;
        s[a]++;
    }
    sort(s + 1, s + m + 1);
    a = s[m] + s[m - 1];
    for (x = s[m]; x <= a; x++) {
        r = 0;
        for (int i = 1, j = m; i <= j;) {
            if (i == j) {
                r++;
                break;
            }
            if (s[i] + s[j] > x) {
                j--;
                r++;
            } else {
                i++;
                j--;
                r++;
            }
        }
        ans = min(ans, x * r);
        // cout << x <<' '<<r <<'\n';
    }
    cout << ans << '\n';
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值