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>
using namespace std;
int n;
int q;
int pref[30][100005];
string s;
vector<pair<int,int> > pa;
int p[100005];
//koriste se prefiksne sume da
//bi za svaki karakter proverilo
//koliko se puta pojavljuje u intervalu l,r
//i zatim se radi binarna po resenju jer ako je neki
//broj od n karaktera dobar posle x grudvi bice dobar i posle x+1
bool check(int x)
{
string z = s;
for(int i=1;i<=x;i++) z[p[i]] = '*';
for(int i=0;i<n;i++)
{
if(z[i]=='*')
{
for(int j =0;j<26;j++)
pref[j][i] = (i-1>=0) ? pref[j][i-1] : 0;
continue;
}
int ind = z[i] - 'a';
for(int j = 0;j<26;j++)
{
if(j==ind)
{
if(i==0) pref[j][i] = 1;
else pref[j][i] = pref[j][i-1] + 1;
}
else
{
if(i==0) pref[j][i] = 0;
else pref[j][i] = pref[j][i-1];
}
}
}
for(auto v : pa)
{
int l = v.first;
int r = v.second;
for(int i=0;i<26;i++)
{
int x;
if(l==0) x = pref[i][r];
else x = pref[i][r] - pref[i][l-1];
if(x > 1)
{
//cout << "lose" << "\n";
//cout << pref[i][r] << " " << pref[i][l-1] << "\n";
//cout << x << "\n";
//cout << l << " " << r << "\n";
return false;
}
}
}
return true;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
cin >> s;
n=s.size();
cin >> q;
for(int i=1;i<=q;i++)
{
int l,r;
cin >> l >> r;
pa.push_back({l-1,r-1});
}
for(int i=1;i<=n;i++)
{
cin >> p[i];
p[i]--;
}
int lo = 0;
int hi = n;
int res = -1;
for(int i=1;i<=30;i++)
{
int mid = (lo+hi)/2;
//cout << mid << "\n";
if(check(mid))
{
res = mid;
hi = mid-1;
}
else lo = mid+1;
}
cout << res;
return 0;
}
# | 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... |
# | 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... |