Codeforces Round #236 (Div. 2) A. Nuts

本文解析了一道关于如何将一定数量的坚果最优地装入有限数量隔板的盒子中的算法题。考虑到每块隔板能增加一个储物格且每个储物格有容量限制,目标是最小化使用的盒子数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A. Nuts
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line contains four space-separated integers kabv (2 ≤ k ≤ 10001 ≤ 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.

Output

Print a single integer — the answer to the problem.

Sample test(s)
input
3 10 3 3
output
2
input
3 10 1 3
output
3
input
100 100 1 1000
output
1
Note

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.

这道题的难点就在于要求的输入太多了,难以理清其中的关系

但是你认真思考的话就会发现首先要考虑b+1 和 k 哪个较大,取较大者可以保证当前对

箱子利用率最大,还要考虑b用完了,只能一个箱子一个箱子装

代码如下:

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#define  MAXN 100
#define  INF 0x7FFFFFFF
#define  ll long long
using namespace std;
int main(void){
	int k, a, b, v;
	while(cin >> k >> a >> b >> v){
		int sum = 0;
		int num = 0;
		while(b != 0){
			
			if(b+1 > k){
				sum += k*v;
				b -= (k-1);
				num++;
			}
			else{
				sum += (b+1)*v;
				b = 0;
				num++;
			}
			if(sum > a)
			 break;
		}
		while(sum < a){
			sum += v;
			num++;
		}
		cout << num << endl;
	}

	return 0;
}



上面的代码跪了。。。原因是while( b! = 0 )的循环内,break的条件应该是sum >= a

就少了一个等号啊

代码如下:
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#define  MAXN 100
#define  INF 0x7FFFFFFF
#define  ll long long
using namespace std;
int main(void){
	int k, a, b, v;
	while(cin >> k >> a >> b >> v){
		int sum = 0;
		int num = 0;
		while(b != 0){
			
			if(b+1 > k){
				sum += k*v;
				b -= (k-1);
				num++;
			}
			else{
				sum += (b+1)*v;
				b = 0;
				num++;
			}
			if(sum >= a)
			 break;
		}
		while(sum<a){
			sum += v;
			num++;
		}
		cout << num << endl;
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值