吃糖果

吃糖果
Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit

Status
Description
HOHO,终于从Speakless手上赢走了所有的糖果,是Gardon吃糖果时有个特殊的癖好,就是不喜欢将一样的糖果放在一起吃,喜欢先吃一种,下一次吃另一种,这样;可是Gardon不知道是否存在一种吃糖果的顺序使得他能把所有糖果都吃完?请你写个程序帮忙计算一下。

Input
第一行有一个整数T,接下来T组数据,每组数据占2行,第一行是一个整数N(0<N<=1000000),第二行是N个数,表示N种糖果的数目Mi(0<Mi<=1000000)。

Output
对于每组数据,输出一行,包含一个"Yes"或者"No"。

Sample Input
2
3
4 1 1
5
5 4 3 2 1

Sample Output
No

Yes


#include<cstdio>
using namespace std;
int main()
{
	int t;
	while (scanf("%d", &t) != EOF)
	{
		for (int i = 0; i < t; i++)
		{
			int n, max = 0;
			long long sum = 0;
			int next = 0;
			scanf("%d", &n);
			for (int j = 0; j < n; j++)
			{
				scanf("%d", &next);
				if (next > max)
					max = next;
				sum += next;
			}
			if (sum - max >= max - 1)
				printf("Yes\n");
			else
				printf("No\n");
		}
	}
}


### C++ 实现糖果递推算法 #### 问题描述 假设有一个孩子每天可以选择一颗糖或多颗糖,但每天的数量有限制。目标是在给定天数内完所有的糖果,并计算有多少种不同的方式可以在这些天内完所有糖果。 #### 动态规划解法 为了求解这个问题,可以采用动态规划的方法。定义 `dp[i][j]` 表示前 `i` 天了 `j` 颗糖果的方式总数。状态转移方程如下: \[ dp[i][j] = \sum_{k=1}^{m} dp[i-1][j-k] \] 其中 \( m \) 是一天最多能糖果数目,\( k \) 是当天可能到的糖果数目。 ```cpp #include <iostream> #include <vector> using namespace std; // 计算在 n 天内完 s 颗糖果的不同方法数 int countWays(int days, int totalCandies, int maxPerDay) { // 创建二维 DP 数组 vector<vector<int>> dp(days + 1, vector<int>(totalCandies + 1, 0)); // 初始条件:第 0 天结束时已经了 0 颗糖果有 1 种情况 dp[0][0] = 1; for (int day = 1; day <= days; ++day) { for (int candies = 0; candies <= totalCandies; ++candies) { for (int eat = 1; eat <= min(candies, maxPerDay); ++eat) { if (candies >= eat && day > 0) { dp[day][candies] += dp[day - 1][candies - eat]; } } } } return dp[days][totalCandies]; } int main() { int days = 3; // 总共多少天 int totalCandies = 5; // 总共有几颗糖果 int maxPerDay = 3; // 每天最多几颗 cout << "Total ways to finish all candies: " << countWays(days, totalCandies, maxPerDay) << endl; return 0; } ``` 此代码实现了上述逻辑并输出了最终的结果。通过调整参数 `days`, `totalCandies` 和 `maxPerDay` 可以测试不同场景下的可能性[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值