游客 Signup | Login
中文 | En

2126 - 3.1.6 Stamps 邮票

3.1.6 Stamps 邮票

(stamps.pas/c/cpp)

<span style="font-size:16px;">&nbsp; 已知一个 N 枚邮票的面值集合(如,{1 分,3 分})和一个上限 K —— 表示信封上能够贴 K 张邮票。计算从 1 到 M 的最大连续可贴出的邮资。</span>

<span style="font-size:16px;">例如,假设有 1 分和 3 分的邮票;你最多可以贴 5 张邮票。很容易贴出 1 到 5 分的邮资(用 1 分邮票贴就行了),接下来的邮资也不难:</span>

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

<span style="font-size:16px;">然而,使用 5 枚 1 分或者 3 分的邮票根本不可能贴出 14 分的邮资。因此,对于这两种邮票的集合和上限 K=5,答案是 M=13。</span>

<span style="font-size:16px;">[规模最大的一个点的时限是3s]</span>

<br />

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

<span class="mw-headline" id=".E6.A0.BC.E5.BC.8F" style="font-size:16px;">格式</span>

<b><span style="font-size:16px;">PROGRAM NAME</span></b><span style="font-size:16px;">: stamps</span>

<b><span style="font-size:16px;">INPUT FORMAT</span></b><span style="font-size:16px;">:</span>

<span style="font-size:16px;">(file stamps.in)</span>

<span style="font-size:16px;">第 1 行: 两个整数,K 和 N。K(1 &lt;= K &lt;= 200)是可用的邮票总数。N(1 &lt;= N &lt;= 50)是邮票面值的数量。</span>

<span style="font-size:16px;">第 2 行 .. 文件末: N 个整数,每行 15 个,列出所有的 N 个邮票的面值,每张邮票的面值不超过 10000。</span>

<b><span style="font-size:16px;">OUTPUT FORMAT</span></b><span style="font-size:16px;">:</span>

<span style="font-size:16px;">(file stamps.out)</span>

<span style="font-size:16px;">第 1 行:一个整数,从 1 分开始连续的可用集合中不多于 K 张邮票贴出的邮资数。</span>

<span class="mw-headline" id="SAMPLE_INPUT" style="font-size:16px;">SAMPLE INPUT</span>

5 2
1 3

<span class="mw-headline" id="SAMPLE_OUTPUT" style="font-size:16px;">SAMPLE OUTPUT</span>

<span style="font-size:16px;">13</span>

Input

Output

Examples

Input


                

Output


                

Solution C

#include<stdio.h>
#include<stdlib.h>
int f[2000001];
int i,j,n,k;
int cost[51];
int max=0;
int min(int u,int u1)
{
    if(u<u1) return u;
    else return u1;
}
int main()
{
    scanf("%d %d",&k,&n);
    int i;   
   
    for(i=1;i<=n;i++)
    {
      scanf("%d",&cost[i]);
      if(cost[i]>max) 
       max=cost[i];
       f[cost[i]]=1;
    } 
    for(i=1;i<=max*k+3;i++)  
      if(f[i]==0)f[i]=327676767;
    for(i=1;i<=max*k+3;i++)
      for(j=1;j<=n;j++)
       if(i-cost[j]>=1)
        f[i]=min(f[i],f[i-cost[j]]+1);
    int ans=0,sum=0;
    while(f[ans+1]<=k)
       ans++;
  printf("%d\n",ans);
 return 0;
}

Solution C++

#include<stdio.h>
#include<string.h>
int dp[2001000];
int min(int x,int y)
{
	return x<y?x:y;	
}
int main()
{
		int n,k,i,j,a[100];
		scanf("%d%d",&k,&n);
		memset(dp,0,sizeof(dp));
		for (i=0;i<n;i++)	
		{
			scanf("%d",&a[i]);
			dp[a[i]]=1;	
		}
		for (i=1;;i++)
		{
			if (dp[i]!=0) continue;
			dp[i]=300;
			for (j=0;j<n;j++)
			{
				if (a[j]<i)
				dp[i]=min(dp[i],dp[i-a[j]]+1);	
			}
			if (dp[i]>k)	break;	
		}
		printf("%d",i-1);
		return 0;
}
Time Limit 1 second
Memory Limit 128 MB
Discuss Stats
上一题 下一题