이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |