内容目录
这题是逆向思维贪心……当时咋没想到……
本题启示:memset的效率与for一遍差不多,故千万memset导致超时……
#include<cstdio> #include<cstring> #include<iostream> #include<cstdlib> #include<cmath> using namespace std; #define MAXN (1000000+10) int n,r,minchange=MAXN; bool ans[MAXN]={0},res[MAXN]; bool flag=0; int calc() { int tot=0; for (int i=2;i<=n;i++) if (!res[i]^res[i-1]) tot++; return tot; } void relax() { /* for (int i=1;i<=n;i++) cout<<res[i]<<' '; cout<<endl; */ int now=calc(); if (!flag||now<minchange) { flag=1;minchange=now; memcpy(ans,res,sizeof(ans)); return; } if (now>minchange) return; for (int i=2;i<=n;i++) { if (ans[i]<res[i]) return ; if (ans[i]>res[i]) { memcpy(ans,res,sizeof(ans)); return; } } } bool is_ok(int l,int r) { // memset(res,0,sizeof(res)); res[1]=1; for (int i=n;i>1;i--) { if (!l||!r) return 0; if (r<l) {l=l-r; res[i]=1;} else {r-=l; res[i]=0;} } if (l!=1||r!=1) return 0; relax(); for (int i=2;i<=n;i++) res[i]=!res[i]; relax(); } int main() { cin>>n>>r; for (int i=1;i<=r;i++) { flag=is_ok(i,r)|flag; //1 cout<<i<<endl; } if (!flag) cout<<"IMPOSSIBLEn"; else { cout<<minchange<<'n'; for (int i=1;i<=n;i++) { if (ans[i]) printf("T"); else printf("B"); } printf("n"); } // while (1); return 0; }