汕頭市隊賽 C KMP codeforces B. Image Preview
阿新 • • 發佈:2017-07-02
++ test href 技術分享 int mage using blog set
汕頭市隊賽題目傳送門
codeforces題目傳送門
這道題我的做法是 嘗試先往左走然後往右走 或者先往右走然後往左走 然後註意一下枚舉順序就okay啦
#include<cstdio> #include<cstring> #include<algorithm> #define LL long long using namespace std; const int M=1e6+7; LL read(){ LL ans=0,f=1,c=getchar(); while(c<‘0‘||c>‘9‘){if(c==‘-‘) f=-1; c=getchar();}View Codewhile(c>=‘0‘&&c<=‘9‘){ans=ans*10+(c-‘0‘); c=getchar();} return ans*f; } int n,a,b,T; LL w[M],ans; char s[M]; LL pd1(int l,int r){ LL sum=w[r]-w[l-1]; int now=(r-l)+(n+1-l); return sum+(LL)now*a; } LL pd2(int l,int r){ LL sum=w[r]-w[l-1]; int now=(r-l)+(r-n-1);return sum+(LL)now*a; } int main() { n=read(); a=read(); b=read(); T=read(); scanf("%s",s+1); for(int i=n+1;i<=2*n;i++) s[i]=s[i-n]; for(int i=1;i<=2*n;i++) w[i]=(s[i]==‘w‘?(b+1):1)+w[i-1]; // for(int i=1;i<=2*n;i++) printf("%lld ",w[i]); printf("\n"); int l=n+1; for(int r=2*n;r>=n+1;r--){ while(l>r-n+1&&pd1(l-1,r)<=T) l--; if(pd1(l,r)<=T) ans=max(ans,(LL)(r-l+1)); } //printf("%lld\n",ans); int r=n+1; for(int l=2;l<=n+1;l++){ while(r<l+n-1&&pd2(l,r+1)<=T) r++; if(pd2(l,r)<=T) ans=max(ans,(LL)(r-l+1)); } // ans=min(ans,(LL)n); printf("%lld\n",ans); return 0; }
汕頭市隊賽 C KMP codeforces B. Image Preview