#include <cstdio> #include <cstdlib> #include <algorithm> int duzo = -10000000; // :) int n,k,t; char *s; int test(int i,int wyjazd, int pomin){ //printf("i=%d wyjazd=%d pomin=%d\n",i, wyjazd, pomin); if(pomin<0){ //printf(" 1 end \n"); return duzo; } if(i>=n) { //printf(" 2 end \n"); return wyjazd==1?duzo:0; } if(s[i]=='1' && wyjazd!=1){ //printf(" 3 end %d\n",i); //pomijamy stacjonarn jak nas tam nie ma return 1+test(i+1,wyjazd,pomin-1); } int add = wyjazd==1?0:1; //nie kombinujemy int t1 = (s[i]=='3'?add:0)+test(i+1, wyjazd, pomin); //omijamy spotkanie int t2 = duzo; if(s[i]!='3'){ t2 = add+test(i+1, wyjazd, pomin-1); } //ruszamy do biura int t3 = duzo; //printf(" ech: i=%d t=%d s[]=%c wyjazd=%d\n",i,t,s[i+t],wyjazd); if(i+t<n && s[i+t]=='1' && wyjazd==0){ //printf("ping111\n"); for (int j = i; j < i+t; ++j) { if(s[j]!='3') { pomin--; //printf("ping\n"); } } t3 = test(i+t,1, pomin); } // jesli "1" i jestesmy wyjechani to wracamy int t4 = duzo; if(/*s[i]=='1' &&*/ wyjazd == 1 && i+1+t<=n){ //printf("ping222 %d\n", i); for (int j = i+1; j < i+1+t; ++j) { if(s[j]!='3') { pomin--; } } t4 = (s[i]=='3'?add:0)+test(i+1+t, 2, pomin); } int r = t1; if(t2>r) r=t2; if(t3>r) r=t3; if(t4>r) r=t4; //printf("i=%d t1=%d t2=%d t3=%d t4=%d r=%d pomin=%d wyjazd=%d\n",i,t1,t2,t3,t4,r, pomin, wyjazd); //return r<0?r:((s[i]=='3'?1:0)+r); return r; } int main(void){ //printf("bu\n"); scanf("%d %d %d", &n, &k, &t); //printf("bu\n"); s = (char *)malloc(n*2 * sizeof(char)); scanf("%s",s); //printf("bu\n"); //printf("ggg"); int r = test(0,0,k); printf("%d\n",r<0?-1:r); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #include <cstdio> #include <cstdlib> #include <algorithm> int duzo = -10000000; // :) int n,k,t; char *s; int test(int i,int wyjazd, int pomin){ //printf("i=%d wyjazd=%d pomin=%d\n",i, wyjazd, pomin); if(pomin<0){ //printf(" 1 end \n"); return duzo; } if(i>=n) { //printf(" 2 end \n"); return wyjazd==1?duzo:0; } if(s[i]=='1' && wyjazd!=1){ //printf(" 3 end %d\n",i); //pomijamy stacjonarn jak nas tam nie ma return 1+test(i+1,wyjazd,pomin-1); } int add = wyjazd==1?0:1; //nie kombinujemy int t1 = (s[i]=='3'?add:0)+test(i+1, wyjazd, pomin); //omijamy spotkanie int t2 = duzo; if(s[i]!='3'){ t2 = add+test(i+1, wyjazd, pomin-1); } //ruszamy do biura int t3 = duzo; //printf(" ech: i=%d t=%d s[]=%c wyjazd=%d\n",i,t,s[i+t],wyjazd); if(i+t<n && s[i+t]=='1' && wyjazd==0){ //printf("ping111\n"); for (int j = i; j < i+t; ++j) { if(s[j]!='3') { pomin--; //printf("ping\n"); } } t3 = test(i+t,1, pomin); } // jesli "1" i jestesmy wyjechani to wracamy int t4 = duzo; if(/*s[i]=='1' &&*/ wyjazd == 1 && i+1+t<=n){ //printf("ping222 %d\n", i); for (int j = i+1; j < i+1+t; ++j) { if(s[j]!='3') { pomin--; } } t4 = (s[i]=='3'?add:0)+test(i+1+t, 2, pomin); } int r = t1; if(t2>r) r=t2; if(t3>r) r=t3; if(t4>r) r=t4; //printf("i=%d t1=%d t2=%d t3=%d t4=%d r=%d pomin=%d wyjazd=%d\n",i,t1,t2,t3,t4,r, pomin, wyjazd); //return r<0?r:((s[i]=='3'?1:0)+r); return r; } int main(void){ //printf("bu\n"); scanf("%d %d %d", &n, &k, &t); //printf("bu\n"); s = (char *)malloc(n*2 * sizeof(char)); scanf("%s",s); //printf("bu\n"); //printf("ggg"); int r = test(0,0,k); printf("%d\n",r<0?-1:r); return 0; } |