您的位置:首页 > 其它

Codeforces 388A Fox and Box Accumulation(贪心)

2014-04-07 10:46 211 查看
题意:给出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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: