USACO-Section 4.1-PROB Beef McNuggets

本文探讨了在给定包装选项下,如何利用数论和动态规划解决最大无法购买数量的问题,通过实例展示了从输入数据到输出结果的完整过程。
Beef McNuggets
Hubert Chen

Farmer Brown's cows are up in arms, having heard that McDonalds is considering the introduction of a new product: Beef McNuggets. The cows are trying to find any possible way to put such a product in a negative light.

One strategy the cows are pursuing is that of `inferior packaging'. ``Look,'' say the cows, ``if you have Beef McNuggets in boxes of 3, 6, and 10, you can not satisfy a customer who wants 1, 2, 4, 5, 7, 8, 11, 14, or 17 McNuggets. Bad packaging: bad product.''

Help the cows. Given N (the number of packaging options, 1 <= N <= 10), and a set of N positive integers (1 <= i <= 256) that represent the number of nuggets in the various packages, output the largest number of nuggets that can not be purchased by buying nuggets in the given sizes. Print 0 if all possible purchases can be made or if there is no bound to the largest number.

The largest impossible number (if it exists) will be no larger than 2,000,000,000.

PROGRAM NAME: nuggets

INPUT FORMAT

Line 1:N, the number of packaging options
Line 2..N+1:The number of nuggets in one kind of box

SAMPLE INPUT (file nuggets.in)

3
3
6
10

OUTPUT FORMAT

The output file should contain a single line containing a single integer that represents the largest number of nuggets that can not be represented or 0 if all possible purchases can be made or if there is no bound to the largest number.

SAMPLE OUTPUT (file nuggets.out)

17


dp+数论

无解:n个数中含有1

无数解:所有数gcd不为1

有一个解:简单的背包dp

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define name "nuggets"
using namespace std;
int dp[100000];
int n,a[12],x;
int gcd(int a,int b)
{
	if (b==0) return a;
	return gcd(b,a%b);
}
int main()
{
	freopen(name ".in","r",stdin);
	freopen(name ".out","w",stdout);
	cin>>n;
	int i,j;
	for (i=1;i<=n;i++)
	{
	    cin>>a[i];
	    if (a[i]==1) {cout<<"0"<<endl;return 0;}
	    if (x==0) x=a[i];else x=gcd(a[i],x);
	    dp[a[i]]=1;
    }
    if (x!=1) {cout<<"0"<<endl;return 0;}
    for (i=1;i<=65536;i++)
	{
		for (j=1;j<=n;j++)
			if (i>=a[j]) dp[i]=dp[i]||dp[i-a[j]];
	}
	for (i=65536;i>=1;i--)
		if (!dp[i]) {cout<<i<<endl;return 0;}
}
/*
Executing...
   Test 1: TEST OK [0.000 secs, 4568 KB]
   Test 2: TEST OK [0.000 secs, 4568 KB]
   Test 3: TEST OK [0.000 secs, 4568 KB]
   Test 4: TEST OK [0.000 secs, 4568 KB]
   Test 5: TEST OK [0.000 secs, 4568 KB]
   Test 6: TEST OK [0.000 secs, 4568 KB]
   Test 7: TEST OK [0.000 secs, 4568 KB]

All tests OK.
YOUR PROGRAM ('nuggets') WORKED FIRST TIME!  That's fantastic
-- and a rare thing.  Please accept these special automated
congratulations.
*/


### USACO 1327 Problem Explanation USACO 1327涉及的是一个贪心算法中的区间覆盖问题。具体来说,这个问题描述了一组奶牛可以工作的班次范围,并要求找出最少数量的奶牛来完全覆盖所有的班次。 对于此类问题的一个有效方法是采用贪心策略[^1]。首先按照区间的结束时间从小到大排序这些工作时间段;如果结束时间相同,则按开始时间从早到晚排列。接着遍历这个有序列表,在每一步都尽可能选择最早能完成当前未被覆盖部分的工作时段。通过这种方式逐步构建最终解集直到所有的时间段都被覆盖为止。 为了提高效率并防止超时错误,建议使用`scanf()`函数代替标准输入流操作符`cin`来进行数据读取处理[^2]。 ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Interval { int start; int end; }; bool compareIntervals(const Interval& i1, const Interval& i2) { return (i1.end < i2.end || (i1.end == i2.end && i1.start < i2.start)); } int main() { vector<Interval> intervals = {{1, 7}, {3, 6}, {6, 10}}; sort(intervals.begin(), intervals.end(), compareIntervals); int currentEnd = 0; int count = 0; for (const auto& interval : intervals) { if (interval.start > currentEnd) break; while (!intervals.empty() && intervals.front().start <= currentEnd) { if (intervals.front().end >= interval.end) { interval = intervals.front(); } intervals.erase(intervals.begin()); } currentEnd = interval.end; ++count; if (currentEnd >= 10) break; // Assuming total shift length is known. } cout << "Minimum number of cows needed: " << count << endl; } ``` 此代码片段展示了如何实现上述提到的方法解决该类问题。需要注意的是实际比赛中可能还需要考虑更多边界条件以及优化细节以满足严格的性能需求。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值