51nod 1246 罐子和硬币

有n个罐子,有k个硬币,每个罐子可以容纳任意数量的硬币。罐子是不透明的,你可以把这k个硬币任意分配到罐子里。然后罐子被打乱顺序,你从外表无法区别罐子。最后罐子被编上号1-n。每次你可以询问某个罐子,如果该罐子里有硬币,则你可以得到1个(但你不知道该罐子中还有多少硬币),如果该罐子是空的,你得不到任何硬币,但会消耗1次询问的机会。你最终要得到至少c枚硬币(c <= k),问题是给定n,k,c,由你来选择一种分配方式,使得在最坏情况下,询问的次数最少,求这个最少的次数。

例如:有3个罐子,10个硬币,需要得到7个硬币,(n = 3, k = 10, c = 6)。
你可以将硬币分配为:3 3 4,然后对于每个罐子询问2次,可以得到6个硬币,再随便询问一个罐子,就可以得到7个硬币了。
Input
输入3个数:n,k,c (1 <= n <= 10^9, 1 <= c <= k <= 10^9)。
Output
输出最坏情况下所需的最少询问次数。
Input示例
4 2 2
Output示例
4


#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <functional>
#include <cmath>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <stack>
using namespace std;
#define esp  1e-8
const double PI = acos(-1.0);
const double e = 2.718281828459;
const int inf = 2147483647;
const long long mod = 1000000007;
//freopen("in.txt","r",stdin); //输入重定向,输入数据将从in.txt文件中读取
//freopen("out.txt","w",stdout); //输出重定向,输出数据将保存在out.txt文件中cin
int main()
{
	int n, k, c, i, j;
	while (~scanf("%d%d%d", &n, &k, &c))
	{
		int x = k / n;
		int y = n - k % n;
		if (x * n >= c || k % n == 0)
			printf("%d\n", c);
		else
		{
			int z = k / (x + 1);
			z = n - z + c;
			//cout << z << endl;
			printf("%d\n", min(z, c + y));
		}
	}
}



你可能感兴趣的:(51nod 1246 罐子和硬币)