题目链接:https://ac.nowcoder.com/acm/contest/886/D
题意:给你 个物品的体积和 个相同体积的盒子,让你将 个物品装进 个盒子,在物品体积小于盒子体积的前提下尽量先装体积较大的物品。问盒子的最小体积。
思路:本题不具有单调性(物品的体积不连续),暴力枚举答案即可。
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 7;
int a[N], b[N], n, k;
multiset <int> s;
multiset<int> ::iterator it;
int check(int x)
{
s.clear();
for(int i = 1; i <= n; i++) s.insert(a[i]);
for(int i = 0; i < k; i++)
{
int v = x;
while(!s.empty() && (it = s.upper_bound(v) ) != s.begin())
{
it--; v -= *it;
s.erase(it);
}
if(s.empty()) return 1;
}
return 0;
}
int main()
{
int T; scanf("%d",&T);
for(int Cas = 1; Cas <= T; Cas++)
{
scanf("%d%d",&n, &k);
int sum = 0, mx = 0;
for(int i = 1; i <= n; i++)
scanf("%d",&a[i]), sum += a[i], mx = max(mx, a[i]);
int ans = max(mx, (int)ceil(1.0 * sum / k));
while(1)
{
if(check(ans))
{
printf("Case #%d: %d\n",Cas, ans);
break;
}
ans++;
}
}
}
/*
1
15 5
39 39 39 39 39 60 60 60 60 60 100 100 100 100 10
*/