呵呵,这个想了半天,参考了一下网上,提交了这个方案:
/* ID: qq104801 LANG: C++ TASK: stamps */ #include <iostream> #include <fstream> #include <cstring> #include <vector> #include <map> #include <list> #include <set> #include <queue> #include <cstdio> #include <algorithm> #include <cmath> using namespace std; #define INF 9999999 int k,n; int v[101]; int f[2000001]={0}; void test() { freopen("stamps.in","r",stdin); freopen("stamps.out","w",stdout); scanf("%d %d",&k,&n); for(int i=1;i<=n;++i) scanf("%d",&v[i]); int min; int i=1; while(true) { min=INF; for(int j=1;j<=n;j++) if((i-v[j])>=0 && (f[i-v[j]]+1<min)) min=f[i-v[j]]+1; if(min>k)break; f[i]=min; ++i; } cout<<i-1<<endl; } int main () { test(); return 0; }
test data:
USACO Training Grader Results 4 users online CHN/2 DNK/1 USA/1 USER: cn tom [qq104801] TASK: stamps LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.005 secs, 11184 KB] Test 2: TEST OK [0.003 secs, 11184 KB] Test 3: TEST OK [0.005 secs, 11184 KB] Test 4: TEST OK [0.005 secs, 11184 KB] Test 5: TEST OK [0.003 secs, 11184 KB] Test 6: TEST OK [0.003 secs, 11184 KB] Test 7: TEST OK [0.003 secs, 11184 KB] Test 8: TEST OK [0.011 secs, 11184 KB] Test 9: TEST OK [0.008 secs, 11184 KB] Test 10: TEST OK [0.054 secs, 11184 KB] Test 11: TEST OK [0.356 secs, 11184 KB] Test 12: TEST OK [0.111 secs, 11184 KB] Test 13: TEST OK [0.016 secs, 11184 KB] All tests OK. Your program ('stamps') produced all correct answers! This is your submission #2 for this problem. 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! Thanks for your submission!