zoukankan      html  css  js  c++  java
  • 洛谷 P2725 邮票 Stamps Label:DP

    题目背景

    给一组 N 枚邮票的面值集合(如,{1 分,3 分})和一个上限 K —— 表示信封上能够贴 K 张邮票。计算从 1 到 M 的最大连续可贴出的邮资。

    题目描述

    例如,假设有 1 分和 3 分的邮票;你最多可以贴 5 张邮票。很容易贴出 1 到 5 分的邮资(用 1 分邮票贴就行了),接下来的邮资也不难:

    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

    然而,使用 5 枚 1 分或者 3 分的邮票根本不可能贴出 14 分的邮资。因此,对于这两种邮票的集合和上限 K=5,答案是 M=13。 [规模最大的一个点的时限是3s]

    小提示:因为14贴不出来,所以最高上限是13而不是15

    输入输出格式

    输入格式:

    第 1 行: 两个整数,K 和 N。K(1 <= K <= 200)是可用的邮票总数。N(1 <= N <= 50)是邮票面值的数量。

    第 2 行 .. 文件末: N 个整数,每行 15 个,列出所有的 N 个邮票的面值,每张邮票的面值不超过 10000。

    输出格式:

    第 1 行:一个整数,从 1 分开始连续的可用集合中不多于 K 张邮票贴出的邮资数。

    输入输出样例

    输入样例#1:
    5 2
    1 3
    输出样例#1:
    13

    说明

    题目翻译来自NOCOW。

    USACO Training Section 3.1

    代码

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cstdlib>
     4 #include<iostream>
     5 #include<algorithm>
     6 #define INF 0x3f3f3f3f
     7 using namespace std;
     8 
     9 int ans,a[55],N,K,f[2000030];
    10 
    11 int main(){
    12     freopen("01.in","r",stdin);freopen("01.out","w",stdout);
    13     
    14     scanf("%d%d",&K,&N);
    15     for(int i=1;i<=N;i++) scanf("%d",&a[i]);
    16     memset(f,0x3f,sizeof(f));f[0]=0;
    17     
    18     for(int i=1;i<=2000010;i++){
    19         for(int k=1;k<=N;k++){
    20             if(i-a[k]<0||f[i-a[k]]==INF||f[i-a[k]]>=K) continue;
    21             f[i]=min(f[i],f[i-a[k]]+1);
    22         }
    23     }
    24     
    25 //    for(int i=0;i<=15;i++) printf("%d ",f[i]);
    26     
    27     for(ans=1;ans<=2050000;ans++) if(f[ans]==INF) break;
    28     printf("%d
    ",ans-1);
    29     
    30     fclose(stdin);fclose(stdout);return 0;
    31 }
    TLE  1个点

    貌似加个特判就可以AC?

    版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!
  • 相关阅读:
    ExecuteScalar requires the command to have a transaction when the connection assigned to the command is in a pending
    如何从vss中分离程序
    String or binary data would be truncated
    the pop3 service failed to retrieve authentication type and cannot continue
    The POP3 service failed to start because
    IIS Error he system cannot find the file specified _找不到页面
    pku2575Jolly Jumpers
    pku2940Wine Trading in Gergovia
    pku3219二项式系数
    pku1029false coin
  • 原文地址:https://www.cnblogs.com/radiumlrb/p/6050518.html
Copyright © 2011-2022 走看看