Title Address: Fzu 2041
This question is one of the questions in the set question of the team in yesterday, I thought the method at that time, but did not dare to write. One is some uncertainty about the complexity, in case the group is a lot of words like kneeling. And feel not very good to achieve, the team still stuck two questions, it is intended to wait for the other out of the problem after the writing, the results have no time.
Just follow that idea to write a bit. The result is over ... I'm so enchanted. I &......%¥%**......%%
The idea is to first enumerate each gap, and then to the left and right to the gap of greedy step by step to move, the rest is a small simulation. Then find out the maximum number of spaces that can be enlarged.
The code is as follows:
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <map>#include <stack>#include <cmath>#define LL __int64#define INF 0x3f3f3f3f#define PI ACOs ( -1)#define EQS 1e-15Const intMoD =1e9+7;using namespace STD;Chars[ -];inta[ -], CNT;structNode {intL, R, X;} fei[ -];intMain () {intT, N, M, I, J, Flag, Lstep, Rstep, Max1, step, case=0, MM;scanf("%d", &t); while(t--) {scanf("%d%d", &n,&mm);scanf('%s ', s+1); s[0]=' 1 '; s[n+1]=' 1 '; Cnt=0; flag=0; max1=0; for(i=0; i<=n+1; i++) {if(s[i]==' 1 ') {if(flag) {flag=0; Fei[cnt].r=i; cnt++; } fei[cnt].l=i; fei[cnt].x=0; }Else{flag=1; fei[cnt].x++; Max1=max (max1,fei[cnt].x); } } for(i=0; i<cnt; i++) {m=mm; for(j=0; j<=n+1; J + +) {a[j]=s[j]-' 0 '; } while(m) {lstep=inf; for(J=FEI[I].L; j>=0; j--) {if(!a[j]) {lstep=fei[i].l-j; Break; }} Rstep=inf; for(J=FEI[I].R; j<=n+1; J + +) {if(!a[j]) {RSTEP=J-FEI[I].R; Break; }} step=min (Lstep,rstep);if(M<step) {Max1=max (FEI[I].X,MAX1); Break; } M-=step; fei[i].x++;if(Lstep<=rstep) {a[fei[i].l]=0; a[fei[i].l-step]=1; fei[i].l--; }Else{a[fei[i].r]=0; a[fei[i].r+step]=1; fei[i].r++; }if(!m) {Max1=max (max1,fei[i].x); Break; } } }printf("Case%d:%d\n", ++CASE,MAX1); }return 0;}
Fzu 2041 Checker (greedy + analog)