QOJ.ac
QOJ
ID | Submission ID | Problem | Hacker | Owner | Result | Submit time | Judge time |
---|---|---|---|---|---|---|---|
#586 | #377604 | #6185. Best Problem | Kevin5307 | Kevin5307 | Success! | 2024-04-05 16:05:30 | 2024-04-05 16:05:31 |
Details
Extra Test:
Wrong Answer
time: 8ms
memory: 42696kb
input:
01
output:
-1139357722199707599
result:
wrong answer 1st numbers differ - expected: '0', found: '-1139357722199707599'
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#377604 | #6185. Best Problem | Kevin5307 | WA | 128ms | 187660kb | C++20 | 5.8kb | 2024-04-05 15:47:29 | 2024-04-05 16:06:08 |
answer
//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
struct segment
{
int lpos,rpos,cnt;
ll sum;
bool type;
segment():sum(-1){}
segment(int _lpos,int _rpos,int _cnt,ll _sum,bool _type):
lpos(_lpos),rpos(_rpos),cnt(_cnt),sum(_sum),type(_type){}
};
ll calc(int cnt,int pos)
{
int r=(cnt-1)*4+pos;
return 1ll*(r+pos)*cnt/2;
}
ll dp[5005000][2];
ll total[5005000];
int main()
{
// freopen("schrodingerszomb12.in","r",stdin);
// freopen("out.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
for(int i=5;i<5005000;i++)
{
int x=(i-1)/4*4;
total[i]+=x;
total[i]+=total[x-1];
}
int t=1;
while(t--)
{
string s;
cin>>s;
s="1"+s+"0";
int n=sz(s);
vector<pii> vec;
for(int i=1;i<n;i++)
if(s[i]==s[i-1])
vec.pb(s[i]^48,i);
// for(auto pr:vec)
// cerr<<"? "<<pr.first<<" "<<pr.second<<endl;
vector<pii> tmp;
ll ssum=0;
for(auto pr:vec)
if(!sz(tmp)||pr.first==0||tmp.back().first==1||(pr.second-tmp.back().second)%4)
tmp.pb(pr);
else
{
ssum+=pr.second-tmp.back().second;
tmp.pop_back();
}
swap(tmp,vec);
// for(auto pr:vec)
// cerr<<"? "<<pr.first<<" "<<pr.second<<endl;
vector<pii> vec2;
vector<int> vb;
vb.pb(0);
for(int i=1;i<sz(vec);i++)
if((vec[i].second-vec[i-1].second)%4)
vb.pb(i);
vb.pb(sz(vec));
vector<segment> cur;
auto add=[&](int cnt,int pos,ll sum,bool type)->void
{
// cerr<<"! "<<cnt<<" "<<pos<<" "<<sum<<" "<<type<<endl;
if(!sz(cur)||cur.back().type!=type)
{
cur.pb(pos,pos+(cnt-1)*4,cnt,sum,type);
return ;
}
if(!type)
{
int rpos=cur.back().rpos;
int need=(pos-rpos-1)/4;
pos-=need*4;
sum-=4ll*cnt*need;
cur.back().rpos=pos+(cnt-1)*4;
cur.back().cnt+=cnt;
cur.back().sum+=sum;
}
else
{
int need=(pos-cur.back().rpos-1)/4;
cur.back().lpos+=need*4;
cur.back().rpos=pos+(cnt-1)*4;
cur.back().sum+=sum+4ll*cur.back().cnt*need;
cur.back().cnt+=cnt;
}
};
for(int i=1;i<sz(vb);i++)
{
int l=vb[i-1],r=vb[i]-1;
// cerr<<"& "<<l<<" "<<r<<endl;
int cnt0=(r-l+1)/2,cnt1=(r-l+1)/2;
if((r-l+1)%2)
{
if(vec[l].first)
cnt1++;
else
cnt0++;
}
if(cnt1)
add(cnt1,vec[l].second,calc(cnt1,vec[l].second),0);
if(cnt0)
add(cnt0,vec[r].second,calc(cnt0,vec[r].second-4*cnt0+4),1);
}
// for(auto seg:cur)
// cerr<<seg.cnt<<" "<<seg.sum<<" "<<seg.lpos<<" "<<seg.rpos<<" "<<seg.type<<endl;
// cerr<<endl;
for(int i=0;i<=sz(cur)+10;i++)
dp[i][0]=dp[i][1]=-0x3f3f3f3f3f3f3f3f;
dp[0][0]=0;
vector<pair<segment,segment>> parts;
for(auto seg:cur)
if(seg.type)
{
if(!sz(parts)||~parts.back().second.sum)
parts.pb(segment(),seg);
else
parts.back().second=seg;
}
else
{
if(!sz(parts)||~parts.back().first.sum||~parts.back().second.sum)
parts.pb(seg,segment());
else
parts.back().first=seg;
}
for(int i=0;i<sz(parts);i++)
{
auto pr=parts[i];
if(pr.first.sum==-1)
{
assert(i==0);
ll val=max(dp[i][0],dp[i][1]);
val+=pr.second.sum;
int len=pr.second.lpos-1;
if(len>4)
{
int cnt=(len-1)/4;
cnt=(cnt+1)/2;
ll R=len-1,L=R-cnt*8;
val+=(L+R)*cnt/2;
}
dp[i+1][0]=max(dp[i+1][0],val);
int rbound=n-1;
if(i!=sz(parts)-1)
rbound=parts[i+1].first.lpos-1;
int mv=(rbound-pr.second.rpos)/4;
pr.second.lpos+=mv*4;
pr.second.sum+=4ll*pr.second.cnt*mv;
val=max(dp[i][0],dp[i][1]);
val+=pr.second.sum;
len=pr.second.lpos-1;
// cerr<<"! "<<len<<endl;
if(len>4)
val+=total[len];
dp[i+1][1]=max(dp[i+1][1],val);
}
else if(pr.second.sum==-1)
{
assert(i==sz(parts)-1);
{
ll val=dp[i][1];
val-=pr.first.sum;
int len=n-pr.first.rpos;
if(len>4)
val+=total[len];
dp[i+1][1]=max(dp[i+1][1],val);
}
{
ll val=dp[i][0];
int lbound=1;
if(i)
lbound=parts[i-1].second.rpos+1;
int mv=(pr.first.lpos-lbound)/4;
pr.first.sum-=4ll*pr.first.cnt*mv;
pr.first.rpos-=4*mv;
val-=pr.first.sum;
int len=n-pr.first.rpos;
if(len>4)
val+=total[len];
dp[i+1][1]=max(dp[i+1][1],val);
}
}
else
{
for(int a=0;a<2;a++)
for(int b=0;b<2;b++)
{
int mvl=0,mvr=0;
if(!a)
{
int lbound=1;
if(i)
lbound=parts[i-1].second.rpos+1;
mvl=(parts[i].first.lpos-lbound)/4;
}
if(b)
{
int rbound=n-1;
if(i!=sz(parts)-1)
rbound=parts[i+1].first.lpos-1;
mvr=(rbound-parts[i].second.rpos)/4;
}
ll val=dp[i][a]-(pr.first.sum-4ll*mvl*pr.first.cnt)+(pr.second.sum+4ll*mvr*pr.second.cnt);
int len=(pr.second.lpos-pr.first.rpos-1);
len+=(mvl+mvr)*4;
if(len>4)
val+=total[len];
dp[i+1][b]=max(dp[i+1][b],val);
}
}
// cerr<<dp[i+1][0]<<" "<<dp[i+1][1]<<endl;
}
ll tot=0;
for(auto pr:vec)
if(pr.first)
tot+=pr.second;
else
tot-=pr.second;
cout<<(dp[sz(parts)][1]+tot+ssum)/4<<'\n';
}
return 0;
}