USACO section 3.1 Stamps(dp)

USACO section 3.4 Closed Fences(计算几何+叉积+二分) Closed Fences A closed fence in the plane is a set of non-crossing, connected line segments with N corners (3 i, yi}, i in (1..N). Every pair of adjacent vertices defines a side of the fence. 阅读详情
Stamps

Given a set of N stamp values (e.g., {1 cent, 3 cents}) and an upper limit K to the number of stamps that can fit on an envelope, calculate the largest unbroken list of postages from 1 cent to M cents that can be created.

For example, consider stamps whose values are limited to 1 cent and 3 cents; you can use at most 5 stamps. It's easy to see how to assemble postage of 1 through 5 cents (just use that many 1 cent stamps), and successive values aren't much harder:

  • 6 = 3 + 3
  • 7 = 3 + 3 + 1
  • 8 = 3 + 3 + 1 + 1
  • 9 = 3 + 3 + 3
  • 10 = 3 + 3 + 3 + 1
  • 11 = 3 + 3 + 3 + 1 + 1
  • 12 = 3 + 3 + 3 + 3
  • 13 = 3 + 3 + 3 + 3 + 1.

However, there is no way to make 14 cents of postage with 5 or fewer stamps of value 1 and 3 cents. Thus, for this set of two stamp values and a limit of K=5, the answer is M=13.

The most difficult test case for this problem has a time limit of 3 seconds.

PROGRAM NAME: stamps

INPUT FORMAT

 

Line 1:Two integers K and N. K (1 <= K <= 200) is the total number of stamps that can be used. N (1 <= N <= 50) is the number of stamp values.
Lines 2..end:N integers, 15 per line, listing all of the N stamp values, each of which will be at most 10000.

SAMPLE INPUT (file stamps.in)

5 2
1 3

OUTPUT FORMAT

 

Line 1:One integer, the number of contiguous postage values starting at 1 cent that can be formed using no more than K stamps from the set.

SAMPLE OUTPUT (file stamps.out)

13


思路:DP题,计算到达n所使用的最小邮票数,如果不能到达或邮票数不够,则结束,答案即是前一个能达到的数。

           dp[i]=min(dp[i-邮票面值]+1);

 

注意点:最早的思路是贪心,后来发现会漏情况。改用DP话说效率还是蛮高的529MS;不过令我很不解的是第2组数据

               1  4

               1 2 3 5

             居然跑了529ms;实在无语。

 

具体看代码

 

/*
ID:nealgav1
LANG:C++
PROG:stamps
*/
#include<fstream>
#include<algorithm>
#include<cstring>
using namespace std;
ifstream cin("stamps.in");
ofstream cout("stamps.out");
const int mm=2000100;
int dp[mm];
int stam[55];
int m,n;
bool cmp(int a,int b)
{
  return a>b;
}
int main()
{
  cin>>m>>n;
  memset(dp,0,sizeof(dp));
  for(int i=0;i<n;i++)cin>>stam[i];
  sort(stam,stam+n,cmp);///排不排序不影响答案
  for(int i=1;i<mm;i++)
  { int num;
    for(int j=0;j<n;j++)
    { num=i-stam[j];
      if(num>=0)
      { ///达到i所使用的最小邮票数
        if(dp[i]==0||dp[i]>dp[num]+1)
        dp[i]=dp[num]+1;
      }
    }///超过有票数,或无法达到则结束,前一个即是答案
    if(!dp[i]||dp[i]>m){cout<<i-1<<"\n";break;}
  }
}


 

 

 

USER: Neal Gavin Gavin [nealgav1]
TASK: stamps
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.022 secs, 11172 KB]
   Test 2: TEST OK [0.529 secs, 11172 KB]
   Test 3: TEST OK [0.022 secs, 11172 KB]
   Test 4: TEST OK [0.022 secs, 11172 KB]
   Test 5: TEST OK [0.022 secs, 11172 KB]
   Test 6: TEST OK [0.022 secs, 11172 KB]
   Test 7: TEST OK [0.022 secs, 11172 KB]
   Test 8: TEST OK [0.022 secs, 11172 KB]
   Test 9: TEST OK [0.022 secs, 11172 KB]
   Test 10: TEST OK [0.076 secs, 11172 KB]
   Test 11: TEST OK [0.335 secs, 11172 KB]
   Test 12: TEST OK [0.097 secs, 11172 KB]
   Test 13: TEST OK [0.022 secs, 11172 KB]

All tests OK.

YOUR PROGRAM ('stamps') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations.

Here are the test data inputs:

------- test 1 ----
5 2
1 3
------- test 2 ----
1 4
1 2 3 5
------- test 3 ----
2 4
1 2 4 8
------- test 4 ----
4 4
1 2 4 8
------- test 5 ----
20 1
1
------- test 6 ----
40 3
5 1 2
------- test 7 ----
3 10
29 50 36 43 1 2 4 8 15 22
------- test 8 ----
6 10
1 2 9 31 255 480 4 15 63 127
------- test 9 ----
8 12
1 2 4 15 9 31 63 127 255 511 1000 1999
------- test 10 ----
200 14
1 2 4 15 9 31 63 2100 3500 127 255 511 1000 1999
------- test 11 ----
200 50
1 37 87 14 137 4016 157 5383 7318 8662 7074 2821 5250 9704 8986
1375 6587 5962 7190 339 1981 9719 7012 385 7926 5159 3259 5144 7846 8854
3249 3198 8408 2784 6249 4648 6799 2757 31 4116 7771 3456 3288 3020 3159
8625 747 9745 938 10000
------- test 12 ----
200 15
1 3 14 59 265 358 979 323 846 26 433 832 7950 28 841
------- test 13 ----
200 15
1 1 1 1 1 10 10 10 10 10 100 100 100 100 100

Keep up the good work!

Stamps
Alex Schwendner

This problem is simply begging for a DP solution. We keep an array "minstamps" such that minstamps[i] is the minimum number of stamps needed to make i cents. For each stamp type, we try adding one stamp of that type to each number of cents that we have already made. After we have found the minimum number of stamps needed to make any given number of cents, we find the smallest number of cents that we cannot make with the given number of stamps, and then we output one less then that.

#include <fstream.h>

const int BIG = 10000;

short   minstamps[10000 * (200 + 3) + 3];
int     stamps;
int     value[200];
int     number;


int 
main ()
{

    ifstream filein ("stamps.in");
    filein >> number >> stamps;
    int     biggest = -1;
    for (int i = 0; i < stamps; ++i) {
	filein >> value[i];
	if (biggest < value[i]) {
	    biggest = value[i];
	}
    }
    filein.close ();

    for (int i = 0; i <= biggest * number + 3; ++i) {
	minstamps[i] = BIG;
    }

    minstamps[0] = 0;
    for (int i = 0; i < stamps; ++i) {
	for (int j = 0; j <= biggest * number; ++j) {
	    if (minstamps[j] < number) {
		if (minstamps[j + value[i]] > 1 + minstamps[j]) {
		    minstamps[j + value[i]] = 1 + minstamps[j];
		}
	    }
	}
    }


    int     string = 0;
    while (minstamps[string + 1] <= number) {
	++string;
    }

    ofstream fileout ("stamps.out");
    fileout << string << endl;
    fileout.close ();

    return (0);
}

 

华大HC32F460从零到200MHz:手把手教你配置时钟系统(附避坑指南) 本文详细解析了华大HC32F460 MCU的时钟系统配置,从启动过程、时钟树分析到实战配置200MHz系统时钟。文章手把手指导开发者如何正确配置外部晶振、MPLL锁相环及总线分频,并提供了硬件检查、代码解读及常见问题排查的避坑指南,帮助用户从零构建稳定可靠的高性能时钟环境。 阅读详情

相关推荐

反制反心理特征的研究

抓牛股

m0_58032574的博客 908

[usaco]3.1.5 stamps

Stamps Given a set of N stamp values (e.g., {1 cent, 3 cents}) and an upper limit K to the number of stamps that can fit on an envelope, ca

马云雷的专栏 907

疲劳容器的定义_FKM规范针对非焊接构件疲劳强度评估实例介绍(下篇)

一、写在前面FKM Inside ANSYS软件,该软件是在FKM 规范的基础上,在ANSYS Workbench内开发的进行非焊接构件及焊缝静强度和疲劳强度评估的工具包。基于ANSYS计算模型的信息,在图形用户界面定义相关设置,利用有限元分析计算结果,自动嵌入FKM规范要求的静强度计算和疲劳强度计算流程和方法,自动将应力结果和相关物理量传递给工具包,无须手动预先选择评估的热点区域,可以...

weixin_35925298的博客 1885

USACO-Section 3.1 Stamps(DP)

这种简单的dp直接能想到做法 提交的代码没有判断i<2000003也AC,没有越界,不过感觉这样更严谨

idealism_xxm的专栏 569

从SolidWorks到URDF:插件使用、关节配置与PyBullet仿真避坑指南

本文提供了一份从SolidWorks导出URDF模型并导入PyBullet进行仿真的完整实战指南。详细讲解了SolidWorks插件的正确安装与配置、关节类型的选择与坐标系对齐的核心技巧,并针对PyBullet仿真中常见的模型散架、路径错误、物理属性异常等问题提供了深度避坑解决方案,帮助机器人开发者高效完成从设计到仿真的全流程。

weixin_29081101的博客 542

【数据分享】1985-2023年30米CLCD土地覆盖栅格数据分享

总的来说,CLCD 反映了中国快速的城市化进程和一系列生态工程,揭示了气候变化条件下人为对 LC的影响,其在全球变化研究中具有潜在应用价值。最后,通过目视解译的独立样本和第三方测试样本评估CLCD的准确性。特别地,该研究将CLCD产品与目前最先进的30m专题产品包括不透水表面积(ISA)、地表水和森林进行比对,以综合评估CLCD的质量(以下为技术流程图)。数据分类:分为九类用地,该数据包括9种用地类型,分别为:农田、森林、灌木、草原、水域、冰雪、裸地、不透水面、湿地。数据格式:.tif格式。

weixin_61129400的博客 6921

usaco Section 3.1 Stamps -- DP

<br />我的大概比较适合初学者看~<br />/* ID: lingxiu1 LANG: C++ TASK: stamps */ #include<stdio.h> #include<string.h> #include<limits.h> #include<iostream> #include<algorithm> using namespace std; int dp[2000001] ;//dp[i]:表示面值为i的邮票需要的最少邮票数 int v[101] ; //面值

. 638

谷歌A2A协议入门指南:附简易Python代码示例

继"关税"之后,当下最热门的词汇当属AI智能体——这种基于人工智能的软件能够执行复杂任务、实现工作流自动化并提升生产效率。但迄今为止,大多数智能体都处于各自为政的状态,难以顺畅协作。就像团队执行重大项目时,成员之间却无法交流!对于技术人员而言,一个可能遇到的典型场景是:假设你需要协调四个不同的AI服务——处理用户查询的Gemini智能体、在向量数据库进行搜索的AI应用、基于Claude的摘要生成器,以及运行专业领域逻辑的Python智能体。

独立开发,substack顶级编辑 796

USACO section 3.1 Stamps

StampsUSACO section 3.1 可行性动态规划

jimmy_the_wanderer的博客 360

把注释掉的代码删除掉:在 SAP ABAP 团队里打造可回溯、可测试、可演进的代码库

摘要 SAP ABAP项目中常见的"注释掉代码"反模式存在严重隐患:它增加代码噪音,制造虚假安全感,并可能引发安全和合规风险。Clean ABAP指南明确建议删除这类代码,理由包括: 版本控制系统已提供完整历史回溯能力 注释代码会误导开发者,增加理解成本 临时禁用需求应通过Feature Toggle实现 必须保留的参考代码应放入$TMP等隔离区域 团队应建立自动化检查机制,将"删除注释代码"纳入工程规范,确保代码库只表达当前有效的行为逻辑。在AI时代,可以利用工具辅

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 2449

【MATLAB例程】三维下的IMM(交互式多模型),模型使用CV(匀速)和CA(匀加速),滤波使用EKF。附完整代码

CV模型:状态包含位置、速度和零加速度(9维状态),状态转移矩阵忽略加速度项CA模型:完整包含位置、速度和加速度(9维状态),状态转移矩阵包含二次项过程噪声矩阵Q分别根据两种模型的运动特性推导。

985工科博士毕业,专攻定位、导航和滤波等算法研究。从业10年,主要使用MATLAB 560

USACO-Section 3.1-PROB Stamps

Stamps Given a set of N stamp values (e.g., {1 cent, 3 cents}) and an upper limit K to the number of stamps that can fit on an envelope, calculate the largest unbroken list of postages from 1 cent

So You Try 468

控制科学与工程(自动化)保研经验【2】——南开、同济篇

禁止转载 1、南开人工智能学院夏令营   保研之旅第一个夏令营就是南开,南开夏令营非常早(6月份),当时我们专业仅有两人入营。夏令营前一天我们两个坐高铁来到了天津,几经辗转来到了南开的津南校区,非常偏僻。在自己找的酒店住了一晚,第二天一早去学院报到,资格审查、报销路费等等,然后开营仪式,吃饭,入住酒店,实验室参观。   开营仪式和实验室参观了解到,南开人工智能学院是国内第一批人工智能学院,和很多高...

Control Everything 6532

USACO Section 3.1 Stamps (stamps)

Stamps Given a set of N stamp values (e.g., {1 cent, 3 cents}) and an upper limit K to the number of stamps that can fit on an envelope, calculate the largest unbroken list of postages from 1 c...

weixin_30713953的博客 144

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

USACO section 3.1 Contact(AC自动机)

Contact IOI'98 The cows have developed a new interest in scanning the universe outside their farm with radiotelescopes. Recently, they noticed a very curious microwave pulsing emission sent right f

nealgavin的专栏 9384

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

USCAO section 1.3 Calf Flac

Calf Flac It is said that if you give an infinite number of cows an infinite number of heavy-duty laptops (with very large keys), that they will ultimately produce all the world's great palindromes

nealgavin的专栏 6656

USACO section 1.5 Checker Challenge(dfs深搜)

Checker ChallengeExamine the 6x6 checkerboard below and note that the six checkers are arranged on the board so that one and only one is placed in each row and each column, and there is never more tha

nealgavin的专栏 1922

USACO section 2.3 Longest Prefix(dp)

Longest Prefix IOI'96 The structure of some biological objects is represented by the sequence of their constituents, where each part is denote by an uppercase letter. Biologists are interested in

nealgavin的专栏 1486

USACO section 2.4 Cow Tours(并查集+最短路)

Cow Tours Farmer John has a number of pastures on his farm. Cow paths connect some pastures with certain other pastures, forming a field. But, at the present time, you can find at least two pasture

nealgavin的专栏 1405

USACO section3.1 Shaping Regions(漂浮法)

Shaping Regions N opaque rectangles (1 <= N <= 1000) of various colors are placed on a white sheet of paper whose size is A wide by B long. The rectangles are put with their sides parallel to the s

nealgavin的专栏 1052

USACO section 2.2 Runaround Numbers

Runaround Numbers Runaround numbers are integers with unique digits, none of which is zero (e.g., 81362) that also have an interesting property, exemplified by this demonstration: If you start

nealgavin的专栏 989

USCAO section 4.1 Fence Loops(最短路,最小环,5级)

Fence Loops The fences that surround Farmer Brown's collection of pastures have gotten out of control. They are made up of straight segments from 1 through 200 feet long that join together only a

nealgavin的专栏 941

USACO section 1.5 Superprime Rib(深搜)

Superprime RibButchering Farmer John's cows always yields the best prime rib. You can tell prime ribs by looking at the digits lovingly stamped across them, one by one, by FJ and the USDA. Farmer John

nealgavin的专栏 937

USACO section 2.2 Party Lamps

Party Lamps IOI 98 To brighten up the gala dinner of the IOI'98 we have a set of N (10 <= N <= 100) colored lamps numbered from 1 to N. The lamps are connected to four buttons: Button 1: Wh

nealgavin的专栏 913

USACO section 3.2 Spinning Wheels(模拟)

Spinning Wheels 1998 ACM NE Regionals Each of five opaque spinning wheels has one or more wedges cut out of its edges. These wedges must be aligned quickly and correctly. Each wheel also has an ali

nealgavin的专栏 875
上一篇: USACO section3.1 Shaping Regions(漂浮法)
下一篇: USACO section 3.1 Contact(AC自动机)
剑不飞
博客等级 码龄15年 86粉丝 505原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值