This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define PI acos(-1)
#define LSB(i) ((i) & -(i))
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define sc second
#define th third
#define fo fourth
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ldb double
#define MOD 1000000007
#define endl "\n"
#define all(data) data.begin(),data.end()
#define TYPEMAX(type) std::numeric_limits<type>::max()
#define TYPEMIN(type) std::numeric_limits<type>::min()
#define ima_li_te(D,d) find(all(D),d)
#define mx 100007
ll psum[3][3][mx],a[mx],b[mx],cnt1[3][mx],cnt2[3][mx];
ll f(char x)
{
if(x=='A') return 0;
else if(x=='C') return 1;
else return 2;
}
void init(string s1, string s2)
{
ll n=s1.size();
for(int i=1;i<=n;i++) a[i]=f(s1[i-1]);
for(int i=1;i<=n;i++) b[i]=f(s2[i-1]);
for(int i=1;i<=n;i++) psum[a[i]][b[i]][i]++;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
for(int k=1;k<=n;k++) psum[i][j][k]+=psum[i][j][k-1];
}
}
for(int i=1;i<=n;i++) cnt1[a[i]][i]++;
for(int i=1;i<=n;i++) cnt2[b[i]][i]++;
for(int i=0;i<3;i++)
{
for(int j=1;j<=n;j++)
{
cnt1[i][j]+=cnt1[i][j-1];
cnt2[i][j]+=cnt2[i][j-1];
}
}
}
int get_distance(int x, int y)
{
x++; y++;
ll rez1=0,rez2=0;
if(!(cnt1[0][y]-cnt1[0][x-1]==cnt2[0][y]-cnt2[0][x-1] && cnt1[1][y]-cnt1[1][x-1]==cnt2[1][y]-cnt2[1][x-1] && cnt1[2][y]-cnt1[2][x-1]==cnt2[2][y]-cnt2[2][x-1])) return -1;
else
{
for(int i=0;i<3;i++)
{
for(int j=i+1;j<3;j++)
{
rez1+=min(psum[i][j][y]-psum[i][j][x-1],psum[j][i][y]-psum[j][i][x-1]);
rez2+=abs((psum[i][j][y]-psum[i][j][x-1])-(psum[j][i][y]-psum[j][i][x-1]));
}
}
return rez1+2*rez2/3;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |