You have a nuts and lots of boxes. The boxes have a wonderful feature: if you put x (x ≥ 0) divisors (the spacial bars that can divide a box) to it, you get a box, divided into x + 1 sections.
You are minimalist. Therefore, on the one hand, you are against dividing some box into more than k sections. On the other hand, you are against putting more than v nuts into some section of the box. What is the minimum number of boxes you have to use if you want to put all the nuts in boxes, and you have b divisors?
Please note that you need to minimize the number of used boxes, not sections. You do not have to minimize the number of used divisors.
The first line contains four space-separated integers k, a, b, v (2 ≤ k ≤ 1000; 1 ≤ a, b, v ≤ 1000) — the maximum number of sections in the box, the number of nuts, the number of divisors and the capacity of each section of the box.
Print a single integer — the answer to the problem.
3 10 3 3
2
3 10 1 3
3
100 100 1 1000
1
In the first sample you can act like this:
- Put two divisors to the first box. Now the first box has three sections and we can put three nuts into each section. Overall, the first box will have nine nuts.
- Do not put any divisors into the second box. Thus, the second box has one section for the last nut.
In the end we've put all the ten nuts into boxes.
The second sample is different as we have exactly one divisor and we put it to the first box. The next two boxes will have one section each.
A 类题目,在CF中A类题相对来说是里面最简单的一类题目。这个题也是。题目分类是Greedy,属于简单贪心。
题目的意思是:
你有一批坚果和一些盒子,盒子有些特别的属性:可以用隔板进行分割,用x个挡板分隔后的空间有 x+1 个区间。每个区间的容量是固定的,一个盒子当做一个空间来看。
输入第一行是四个用空格分隔的数据,k,a,b,v k 是一个最大能够分隔的空间,a 是坚果的数量, b 是总的挡板的数量, v 是每一个空间的容量。
我的思路:
将题目分成两部分,一部分是挡板分隔的盒子能够装完全部的盒子,一种是不能装完(不能装完的可以分两种,一种是还剩下不能完全分隔盒子的挡板,已经没有分隔的盒子),其实这个并不能算贪心的做法,不过也有贪心的思想在里面:就完全的盒子先装,然后再装不完全分离的盒子。
另一种做法是,将每一个盒子的容量放到一个数组里面去,然后循环的去放坚果,然后盒子数量往后面加。知道容量大于坚果数,循环就结束了。
下面是我初步写出的代码,代码很烂:
/*codeforces Problem 402A - A nuts
***************************
k is the maximum number of sections in one box
a is the number of nuts
b is the number of divisors
v is then capacity of one section
one box you can think it is a section
****************************/
#include <stdio.h>
int main(){
int k, a, b, v;
int bcnt = 0;
scanf("%d%d%d%d", &k, &a, &b, &v);
int bn = b / (k-1);
if(a <= bn*k*v){
if(a % v == 0){
bcnt = a / (v*k);
} else {
bcnt = a / (v*k) + 1;
}
} else{
bcnt = bn;
a -= bn * k * v;
b -= bn * (k - 1);
if(a <= (b+1)*v)
bcnt++;
else{
a -= (b+1)*v;
bcnt++;
if(a % v == 0)
bcnt += a / v;
else
bcnt += (a / v +1);
}
}
printf("%d", bcnt);
return 0;
}