Submission #301913

#TimeUsernameProblemLanguageResultExecution timeMemory
301913milisavMonochrome Points (JOI20_monochrome)C++14
100 / 100
767 ms5640 KiB
#include<bits/stdc++.h> #pragma GCC optimize("O3") #define maxn 1000000 using namespace std; int n; long long ans=0; char s[2*maxn]; int d[2*maxn]; int bit[8*maxn]; void update(int p,int v) { while(p<=2*n) { bit[p]+=v; p+=(p&(-p)); } } int query(int p) { int ret=0; while(p>0) { ret+=bit[p]; p-=(p&(-p)); } return ret; } int l[maxn]; int r[maxn]; long long val(int i) { int cb=0; int x=0; int y=0; long long cans=0; int tmp=0; while(cb<i || s[y]!='B') { if(s[y]=='B') cb++; y++; if(y==2*n) y=0; } int k=1; l[k]=x; r[k]=y; d[x]=-k; d[y]=k; x++; while(x<2*n) { if(s[x]=='W') { k++; do { y++; if(y==2*n) y=0; } while(s[y]!='B'); l[k]=min(x,y); r[k]=max(x,y); d[min(x,y)]=-k; d[max(x,y)]=k; } x++; } for(int i=0;i<2*n;i++) { if(d[i]>0) { update(l[d[i]]+1,-1); cans+=query(r[d[i]]+1)-query(l[d[i]]+1); } else update(i+1,1); } return cans; } int main() { scanf("%d",&n); scanf("%s",s); if(s[0]=='B') { for(int i=0;i<2*n;i++) { if(s[i]=='B') s[i]='W'; else s[i]='B'; } } int l=0,r=n-1; while(l<r) { int m1=(l+r)>>1; int m2=m1+1; long long v1=val(m1); long long v2=val(m2); ans=max(ans,v1); ans=max(ans,v2); if(v1>v2) r=m2-1; else l=m1+1; } ans=max(ans,val(l)); printf("%lld",ans); return 0; }

Compilation message (stderr)

monochrome.cpp: In function 'long long int val(int)':
monochrome.cpp:31:9: warning: unused variable 'tmp' [-Wunused-variable]
   31 |     int tmp=0;
      |         ^~~
monochrome.cpp: In function 'int main()':
monochrome.cpp:67:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   67 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
monochrome.cpp:68:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   68 |     scanf("%s",s);
      |     ~~~~~^~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...