[USACO5.3] 量取牛奶Milk Measuring(c++)提高T4/省选

代码君,上线。

题目(英文):

Farmer John would measure Q (1⩽ ⩽20,0001⩽Q⩽20,000) quarts of his best milk, put it into a large bottle and sell it. He gives consumers as much as they want, and there is never any error.

Farmer John is always thrifty. He now buys buckets at the cow hardware store to measure out Q quarts of milk from his huge milk pool. The price of each bucket is the same. Your task is to figure out the minimum set of buckets that Farmer John can buy in order to measure exactly Q quarts of milk out of those buckets. In addition, since Farmer John must carry the buckets home, given two sets of extremely small buckets, he will choose the "smaller" one, that is, sorting the two sets in ascending order, comparing the first bucket to the one with the smaller volume. If the first bucket is the same, compare the second bucket and select the same as above. Otherwise, the work continues until the two buckets compared do not match. For example, set,5,7,100 {3} {3,5,7,100} than collection,6,7,8 {3} {3,6,7,8} is better.

To measure out the milk, Farmer John could fill the pail from the milk pool and pour it into the bottle. He never pours the milk out of the bottle or the milk out of the pail. Using a bucket with a volume of 11 quarts, Farmer John could measure out all possible quarts using only this bucket. Other bucket combinations are not as convenient.

Calculate the optimal set of buckets to purchase to ensure that all test data has at least one solution.

描述(中文):

农夫约翰要量取 �Q(1⩽�⩽20,0001⩽Q⩽20,000)夸脱(译注:即 quarts,容积单位)他的最好的牛奶,并把它装入一个大瓶子中卖出。消费者要多少,他就给多少,从不有任何误差。

农夫约翰总是很节约。他现在在奶牛五金商店购买一些桶,用来从他的巨大的牛奶池中量出 �Q 夸脱的牛奶。每个桶的价格一样。你的任务是计算出一个农夫约翰可以购买的最少的桶的集合,使得能够刚好用这些桶量出 �Q 夸脱的牛奶。另外,由于农夫约翰必须把这些桶搬回家,对于给出的两个极小桶集合,他会选择“更小的”一个,即:把这两个集合按升序排序,比较第一个桶,选择第一个桶容积较小的一个。如果第一个桶相同,比较第二个桶,也按上面的方法选择。否则继续这样的工作,直到相比较的两个桶不一致为止。例如,集合 {3,5,7,100}{3,5,7,100} 比集合 {3,6,7,8}{3,6,7,8} 要好。

为了量出牛奶,农夫约翰可以从牛奶池把桶装满,然后倒进瓶子。他决不把瓶子里的牛奶倒出来或者把桶里的牛奶倒到别处。用一个容积为 11 夸脱的桶,农夫约翰可以只用这个桶量出所有可能的夸脱数。其它的桶的组合没有这么方便。

计算需要购买的最佳桶集,保证所有的测试数据都至少有一个解。

要求 :

通过率 67%    时间限制 1000ms    内存限制 256MB

输入

第一行一个整数 �Q。

第二行一个整数 � (1⩽ �⩽100)P (1⩽ P⩽100),表示商店里桶的数量。

第 33 至第 �+2P+2 行,每行一个整数 ℎ�hi​ 表示第 �i 个桶的容积。(1⩽ℎ�⩽104)(1⩽hi​⩽104)。

输出

输出文件只有一行,由空格分开的整数组成:

为了量出想要的夸脱数,需要购买的最少的桶的数量,接着是:

一个排好序的列表(从小到大),表示需要购买的每个桶的容积

题解:

考[动态规划][搜索]

动态规划(动态规划_百度百科 (baidu.com)):

动态规划(Dynamic Programming,DP)是运筹学的一个分支,是求解决策过程最优化的过程。20世纪50年代初,美国数学家贝尔曼(R.Bellman)等人在研究多阶段决策过程的优化问题时,提出了著名的最优化原理,从而创立了动态规划。动态规划的应用极其广泛,包括工程技术、经济、工业生产、军事以及自动化控制等领域,并在背包问题、生产经营问题、资金管理问题、资源分配问题最短路径问题和复杂系统可靠性问题等中取得了显著的效果。

搜索(代码解释):

//算法框架
void dfs(int n){
   if(搜索结束){
      记录结果。
      return;
   }
   for(遍历所有解){
       if(合法的解){
         占位。
    }
       dfs(n+1);
       取消占位。
  }
}

就这些,AC代码(内有批注):

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAX 200
#define rg register 
int Q,p;
int v[MAX];//体积
bool fl=false;//标记是否找到解 
int dep;//指定的深度 
int C[MAX];//记录选定的桶 
bool f[MAX*MAX];//DP用 
inline bool ok()
{
       //memset(f,0,sizeof(f));
       for(rg int i=1;i<=Q;++i)
         f[i]=false;
       f[0]=true;
       for(rg int i=1;i<=Q;++i)
                for(rg int j=0;j<dep&&!f[i];++j)
                 if(i>=C[j])
                    f[i]|=f[i-C[j]];
                 else
                 break; 
       return f[Q];
}
inline void outp()
{
       cout<<dep<<' ';
       for(rg int i=0;i<dep;++i)
         cout<<C[i]<<' ';
       cout<<endl;       
}
void DFS(rg int x,rg int tot)//上个桶的编号以及已经选的桶的数量 
{
       if(tot==dep)
       {
                if(ok())
                {
                       outp();
                       fl=true;
                }
                return;
       }
       for(rg int i=x+1;i<=p&&!fl;++i)
       {
                 if(p-i+1<dep-tot)//桶的个数不够到达指定深度 
                    break;
                 C[tot]=v[i];
              DFS(i,tot+1);
              C[tot]=0;
       } 
}
int main()
{
      scanf("%d%d",&Q,&p);
      for(rg int i=1;i<=p;++i)
        scanf("%d",&v[i]);
      sort(&v[1],&v[p+1]);
      for(rg int i=1;i<=p;++i)//迭代加深
      {
               dep=i;//最大深度
             DFS(0,0); 
             if(fl)break;
      }
      return 0;
}//完结撒花

下次可以用 未来新科技——Sora来写:

Sora - 探索AI视频模型的无限可能

随着人工智能技术的飞速发展,AI视频模型已成为科技领域的新热点。而在这个浪潮中,OpenAI推出的首个AI视频模型Sora,以其卓越的性能和前瞻性的技术,引领着AI视频领域的创新发展。让我们将一起探讨Sora的技术特点、应用场景以及对未来创作方式的深远影响。
提醒:在发布作品前,请把不需要的内容删掉。

方向一:技术解析

提示:深入探讨Sora的技术架构、算法原理以及实现过程。通过专业性的文章或视频,向读者和观众展示Sora是如何通过深度学习和自然语言处理技术实现视频内容的智能生成和互动的。

方向二:应用场景

提示:想象并描述Sora在不同领域的应用场景,如影视制作、广告创意、游戏设计、在线教育等。通过故事性的叙述或案例分析,展示Sora如何为这些领域带来革命性的变革。

方向三:未来展望

提示:预测并讨论Sora对未来数字内容创作方式的影响。分析在AI视频模型的助力下,创作者们将如何突破传统限制,实现更加个性化、高效和创新的创作过程。

方向四:伦理与创意

提示:探讨在AI技术日益普及的背景下,如何平衡技术创新与伦理道德的关系。讨论Sora等AI视频模型在提升创意效率的同时,如何尊重原创精神、保护知识产权等问题。

方向五:用户体验与互动

提示:分析Sora如何提升用户体验和互动性。探讨在AI技术的驱动下,视频内容将如何更加智能地适应用户需求,实现更加自然和高效的人机交互。

  • 50
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值