Codeforces Round #228 (Div. 1)A. Fox and Box Accumulation(逆向思维)
2017-07-31 12:35
417 查看
题目链接
http://codeforces.com/problemset/problem/388/A题目大意
给你n个盒子,以及其上面最多能放的盒子个数ai, 把这些盒子放成几堆(一层只能有一个盒子), 问你最少放成几堆?思路
我们发现如果从大的盒子往上面放盒子进行模拟总是会有bug, 而且写起来很麻烦所以我们逆向思维, 先放小的
我们把堆从上到下标号0~x, 那么
第0层最少可以放ai=0的盒子
第1层最少可以放ai=1的盒子
第2层最少可以放ai=2的盒子
。
。
。
第x层最少可以放ai=x的盒子
把ai排序
得到这个规律,我们可以发现一旦发现第i层放的盒子小于ai,那么就一定要重新放一堆, 设堆数为ans, 那么这时候第i层放的盒子下限就提高到了ans * ai(重点理解,可以试着模拟一个数据试试)
这就避免了从大到小放出现该什么时候分堆的问题
代码
#include<bits/stdc++.h> using namespace std; int a[105]; int main() { int n; scanf("%d", &n); for(int i=0; i<n; ++i) scanf("%d", a+i); sort(a, a+n); int ans = 0; for(int i=0; i<n; ++i) { if(ans*(a[i]+1)<=i)++ans; } printf("%d\n", ans); return 0; }
反思
比赛的时候想了很长时间才想到, 思维实在太僵硬了当正面解决遇到问题时, 反过来思考往往能使问题变得简单
相关文章推荐
- Codeforces #228 (Div. 2)C. Fox and Box Accumulation
- Codeforces Round #228 (Div. 2) C. Fox and Box Accumulation
- Codeforces Round #228 (Div. 1) A. Fox and Box Accumulation 贪心
- Codeforces Round #228 (Div. 1) A. Fox and Box Accumulation
- Codeforces Round #228 (Div. 1) A. Fox and Box Accumulation 贪心
- Codeforces Round #228 (Div. 2)C. Fox and Box Accumulation
- Codeforces Round #228 (Div. 2)C. Fox and Box Accumulation
- cf228 div2 C. Fox and Box Accumulation(贪心, 二分)
- Codeforces Round #228 (Div. 2) C Fox and Box Accumulation(贪心)
- 贪心-codeforces-388A-Fox and Box Accumulation
- CF228C题Fox and Box Accumulation
- A. Fox and Box Accumulation
- [Codeforces] 389C - Fox and Box Accumulation
- codeforces389 C. Fox and Box Accumulation【简单贪心】
- CF 388A:Fox and Box Accumulation
- cf C. Fox and Box Accumulation
- Fox and Box Accumulation
- 【贪心+简单题】-CF-388A-A fox and box accumulation
- A. Fox and Box Accumulation
- Fox and Box Accumulation CodeForces - 388A