Codeforces 388A Fox and Box Accumulation(贪心)

来源:互联网 发布:淘宝网上的汽车能买吗 编辑:程序博客网 时间:2024/06/05 15:26

题意:给出n个箱子,每个箱子都有一个力量值Vi,代表能支撑Vi个箱子,求能把这些箱子摆成的最少的堆数.

思路:刚开始想着从大到小排序来做,从第i个箱子开始能放上去的就放上去,题目的最后一个数据很好的否定了这种做法.

应该从小到大排序,记录当前堆的个数,能否把当前堆放到下一个箱子上,不能放就作为一堆.

#include <cstdio>#include <functional>#include <algorithm>using namespace std;const int MAX = 101;int boxes[MAX];bool vis[MAX];int main(int argc, char const *argv[]){int n, ans = 0;scanf("%d", &n);for(int i = 0; i < n; ++i){scanf("%d", &boxes[i]);}sort(boxes, boxes + n);for(int i = 0; i < n; ++i){if(vis[i])continue;int cnt = 1;for(int j = i + 1; j < n; ++j){if(!vis[j] && boxes[j] >= cnt){cnt++;vis[j] = true;}}ans++;}printf("%d\n", ans);return 0;}


0 0
原创粉丝点击