标签:main strong put ext def name 序列 include push
大箱子能装小箱子,求在满足最少箱子的情况下,最小化每个箱子中最大的箱子个数.
想到二分枚举箱子数,然后贪心的选择放进箱子的位置.
最优策略一定是将最大的 \(m\) 个先找出来,然后把剩下的放到这 \(m\) 个中,
我们发现子问题和上述问题是一样的, 所以贪心策略不变.
只需要判断这样贪心出来会不会有违反递减序列性质的就行了
注意: 一定要每个 case 间输出空格, 数的末尾不能有多余空格, 不然会 WA
#include <bits/stdc++.h>
using namespace std;
#define rep(i, s, t) for (int i = (int)(s); i <= (int)(t); ++ i)
const int maxn = 1e4 + 10;
int a[maxn], n;
vector<int> b[maxn];
bool check(int m) {
for (int i = 0; i < maxn; ++ i) b[i].clear();
for (int i = 1; i <= m; ++ i) b[i].push_back(a[i]);
for (int i = m + 1; i <= n; ++ i) {
int next = (i - 1) % m + 1;
if (a[i] < *(b[next].end() - 1)) b[next].push_back(a[i]);
else return false;
}
return true;
}
int main() {
while (~scanf("%d", &n) && n) {
rep(i, 1, n) scanf("%d", a + i);
sort(a + 1, a + n + 1); reverse(a + 1, a + n + 1);
int l = 1, r = n, mid;
while (l < r) {
if (check(mid = (l + r) >> 1)) r = mid; else l = mid + 1;
}
check(l); printf("%d\n", l);
vector<int>::iterator it;
for (int i = 1; i <= l; ++ i) {
sort(b[i].begin(), b[i].end());
printf("%d", *b[i].begin());
for (it = b[i].begin()+1; it != b[i].end(); ++ it) {
printf(" %d", *it);
}
putchar(10);
}
putchar(10);
}
}
标签:main strong put ext def name 序列 include push
原文地址:https://www.cnblogs.com/Alessandro/p/10011558.html