USACO 3.1 Stamps
和前边的一个题目求前缀那个很像,加上一个标记数组就行。
1 /* 2 ID: cuizhe 3 LANG: C++ 4 TASK: contact 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <iostream> 9 #include <queue> 10 #include <map> 11 using namespace std; 12 #define N 200001 13 int dp[2000001],p[500]; 14 bool o[2000001]; 15 int Min(int a,int b) 16 { 17 return a > b ? b:a; 18 } 19 int main() 20 { 21 int i,j,n,k; 22 //freopen("stamps.in","r",stdin); 23 //freopen("stamps.out","w",stdout); 24 scanf("%d%d",&k,&n); 25 for(i = 1;i <= n;i ++) 26 { 27 scanf("%d",&p[i]); 28 o[p[i]] = 1; 29 dp[p[i]] = 1; 30 } 31 for(i = 1;;i ++) 32 { 33 if(o[i]&&dp[i] < k) 34 { 35 for(j = 1;j <= n;j ++) 36 { 37 o[i+p[j]] = 1; 38 if(dp[i+p[j]] == 0) 39 dp[i+p[j]] = dp[i]+1; 40 else 41 dp[i+p[j]] = Min(dp[i]+1,dp[i+p[j]]); 42 } 43 } 44 else if(!o[i]) 45 { 46 printf("%d\n",i-1); 47 break; 48 } 49 } 50 return 0; 51 }