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);
}

 

【大唐杯学习超快速入门】5G技术原理仿真教学——5G网络开通调测与车联网 自用笔记,比较乱。 阅读详情

相关推荐

Windows下QT5.15.2安装MQTT模块全攻略(附分支选择避坑指南)

本文详细介绍了在Windows环境下为QT5.15.2安装MQTT模块的完整流程,包括环境准备、源码获取、编译配置及项目集成等关键步骤。特别针对分支选择、依赖项处理和常见错误提供了实用解决方案,帮助开发者高效完成MQTT模块的安装与调试,适用于物联网开发场景。

weixin_29224589的博客 472

[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

VMware ESXI 7.0实战:4T硬盘RAID1配置与性能调优全记录

本文详细记录了在VMware ESXI 7.0环境下,为Dell服务器配置两块4T硬盘组建RAID1并进行深度性能调优的全过程。文章重点解析了RAID控制器写入策略(Write-Back with BBU)、读取策略(Read-Ahead)的关键选择,并深入探讨了在ESXI虚拟机层面通过选用PVSCSI控制器、调整虚拟磁盘缓存策略(scsiX:Y.caching)等技巧,以最大化满足数据分析等IO密集型工作负载的需求,最终实现性能的显著提升。

gaochao的博客 268

USACO-Section 3.1 Stamps(DP)

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

idealism_xxm的专栏 569

传输线末端短路、开路、加电感的反射系数

时,频率变化约300MHz, 出现这种偏差是因为我们的计算中将电磁波传播速率默认为。时,频率变化约300MHz, 出现这种偏差是因为我们的计算中将电磁波传播速率默认为。下图为用网络分析仪实测一长度为30cm的传输线末端加一0.6uH的线圈的反射系数。下图为用网络分析仪实测一末端短路的长度为30cm的传输线的反射系数。下图为用网络分析仪实测一末端开路的长度为30cm的传输线的反射系数。末端加电感相当于在传输线末端延长了一段后末端短路,使得相位下降速率。幅度为0dB, 相位由。幅度为0dB, 相位由。

johnboat的专栏 1882

OpenLayers6入门,快速搭建Vue+OpenLayers地图脚手架项目

本章针对Vue初学者,对Vue不熟悉,甚至还不会Vue的入门学生读者。本章会详细讲解从环境到环境的各个步骤,再到使用脚手架快速生成项目,以及添加地图库依赖,编写简单的高德地图显示页面的。如果已有VUE项目或者熟悉VUE的读者可以不用看本章,直接添加ol依赖到VUE项目中即可。

GIS技术方向,GIS地图技术分享(GIS入门、Openlayers教程、Leaflet教程),做最好的GIS地图开发教程。关注博主公众号:eguid 5451

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] ; //面值

. 640

PyTorch深度学习实战(3)——使用PyTorch构建神经网络

PyTorch 是一个用于构建深度神经网络的库,具有灵活性和可扩展性,可以轻松自定义模型。在本节中,我们将使用 PyTorch 库构建神经网络,利用张量对象操作和梯度值计算更新网络权重,并利用 Sequential 类简化网络构建过程,最后还介绍了如何使用 save、load 方法保存和加载模型,以节省模型训练时间。

盼小辉丶的博客 8万+

USACO section 3.1 Stamps

StampsUSACO section 3.1 可行性动态规划

jimmy_the_wanderer的博客 360

Fast-Lio2代码及论文解析2

点云数据频率较低而IMU为高频数据。

努力努力努力 2283

华为面试手撕真题【判断是ipv4还是ipv6地址】

编写一个函数来验证输入的字符串是否是有效的 IPv4 或 IPv6 地址。

MISAYAONE的博客 1万+

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

【DaVinci Developer专题】-8-创建一个枚举Enum ADT(Application Data Type)

目录 1 创建一个枚举Value ADT(Application Data Type) 1.1 命名application data type 1.2 创建一个computation method计算方法属性 1.3 创建一个Contraint约束条件属性 2 创建了一个新的Type Mapping Sets 3 创建了一个S/R Port Interface 4 SWC引用Type Mapping set 5 导入CFG生成代码的总结 6 结尾 <--返回「实战篇」博文-总目录-

「汽车电子助手」的博客 2301

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

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

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

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

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

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

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的专栏 1924

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的专栏 1487

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的专栏 1406

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的专栏 991

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的专栏 938

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的专栏 914

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、付费专栏及课程。

余额充值
⌂ 换网址