【贪心+简单题】-CF-388A-A fox and box accumulation
2014-02-05 15:17
495 查看
题目链接:http://codeforces.com/problemset/problem/388/A
题目描述:
每个箱子都有一个为非负整数的强度系数——意味着这个箱子上面最多还能叠放几个箱子。先给出若干个箱子和它们对应的强度系数,问至少可以堆成几摞。
解题思路:
一开始以为是什么高大上的背包问题,想想就没思路不敢做了。后来才发现是道水题,不过还是要动下脑子的。(其实有点贪心的意思)——把所有箱子按强度系数排序。先取强度最小的箱子,并设一个变量 t 用来记录已经放在上方的箱子个数,遍历一遍判断对于箱子 i 是否能承载住 t 个箱子的重量,如果能,这个箱子 i 就也可以加入到上方的箱子中。用过的箱子用 -1 做标记,遍历若干遍后得到答案。箱子不多,这是一个时间上比 o(n^2)还小的算法,完全可行。
AC代码:
AC截图:
![](http://img.blog.csdn.net/20140205151607656?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3J5c3NkdXQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
题目描述:
每个箱子都有一个为非负整数的强度系数——意味着这个箱子上面最多还能叠放几个箱子。先给出若干个箱子和它们对应的强度系数,问至少可以堆成几摞。
解题思路:
一开始以为是什么高大上的背包问题,想想就没思路不敢做了。后来才发现是道水题,不过还是要动下脑子的。(其实有点贪心的意思)——把所有箱子按强度系数排序。先取强度最小的箱子,并设一个变量 t 用来记录已经放在上方的箱子个数,遍历一遍判断对于箱子 i 是否能承载住 t 个箱子的重量,如果能,这个箱子 i 就也可以加入到上方的箱子中。用过的箱子用 -1 做标记,遍历若干遍后得到答案。箱子不多,这是一个时间上比 o(n^2)还小的算法,完全可行。
AC代码:
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace std; int arr[120]; int main() { int n,i,j,t,ans=0; scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&arr[i]); sort(arr,arr+n); for(i=0;i<n;i++) { if(arr[i]>=0) { ans++; t=1; arr[i]=-1; for(j=i+1;j<n;j++) { if(arr[j]>=t) { t++; arr[j]=-1; } } } } cout<<ans<<endl; return 0; }
AC截图:
相关文章推荐
- 贪心-codeforces-388A-Fox and Box Accumulation
- codeforces389 C. Fox and Box Accumulation【简单贪心】
- CF 388A:Fox and Box Accumulation
- codeforces-388A-Fox and Box Accumulation
- Fox and Box Accumulation CodeForces - 388A
- Codeforces Round #228 (Div. 1) A. Fox and Box Accumulation 贪心
- cf228 div2 C. Fox and Box Accumulation(贪心, 二分)
- Codeforces Round #228 (Div. 1) A. Fox and Box Accumulation 贪心
- cf C. Fox and Box Accumulation
- Codeforces Round #228 (Div. 2) C Fox and Box Accumulation(贪心)
- Codeforces Round #228 (Div. 2)C. Fox and Box Accumulation
- Codeforces Round #228 (Div. 2) C. Fox and Box Accumulation
- CF - 389 - B. Fox and Cross(贪心)
- Codeforces Round #228 (Div. 2)C. Fox and Box Accumulation
- CF 628C --- Bear and String Distance --- 简单贪心
- A. Fox and Box Accumulation
- A. Fox and Box Accumulation
- Codeforces Round #228 (Div. 1)A. Fox and Box Accumulation(逆向思维)
- Codeforces #228 (Div. 2)C. Fox and Box Accumulation
- [Codeforces] 389C - Fox and Box Accumulation