We'd like to thank you all for participating in the contest, and hope you enjoyed it. Hope to see you again soon!
Author: JainWinn
There are $$$26$$$ lowercase alphabets, and we want $$$7$$$ as maximum frequency among all these characters. What would be maximum length of a Thala substring?
We will use two-pointer approach to solve the problem.Left pointer can be fixed (say $$$i$$$).
There are $$$26$$$ lowercase alphabets, and we want $$$7$$$ as maximum frequency among all these characters. So in worst case we will check for at most $$$26 \times 7 = 182$$$ size substring.
So for each $$$i$$$, our right pointer(say $$$j$$$) will iterate from $$$i$$$ to $$$min(i+26 \times 7,n)$$$ i.e $$$(i \leq j \lt min(i+26 \times 7,n)$$$ .Keep track of maximum frequency(using array/vector of size 26) starting from each $$$i$$$ and increment answer whenever maximum frequency equals $$$7$$$.
The overall Time Complexity would be : $$$O((26 \times 7) N)$$$
#include<bits/stdc++.h>
using namespace std;
//=======================================================================
#define ll long long
#define int long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int,int>
#define vpi vector<pair<int,int>>
#define all(x) (x).begin(),(x).end()
#define countbits(x) __builtin_popcount(x)
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
int mod = 1e9+7;
//int mod = 998244353;
void solve(){
int n;
cin>>n;
string s;
cin>>s;
int ans=0;
//stores freq of characters
vi mp(26,0);
for(int i=0 ; i<n ; i++){
//stores maximum frequency among all characters
int maxi=0;
for(int j=i ; j<min(i+26*7,n) ; j++){
mp[s[j]-'a']++;
maxi=max(maxi,mp[s[j]-'a']);
//increasing answer if maximum frequency is 7
ans+=(maxi==7);
}
for(int j=i ; j<min(i+26*7,n) ; j++){
mp[s[j]-'a']--;
}
}
cout<<ans<<endl;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}
E – Geometric Progression Subsequence
Author: JainWinn
Fixing one among $$$(l,r)$$$, could you find the other one?
We can utilize dynamic programming to keep track of the rightmost indices for elements $$$(1 \leq A_i \leq 30)$$$.This will help us find the rightmost $$$l$$$ for each $$$r$$$ by fixing $$$r$$$ and iterating over all geometric progressions where $$$A_k = A_r$$$ and finding $$$l_{max}[r]= max(l_{max}[r-1]$$$, all $$$i$$$ where $$$A_i \times A_k = A_j \times A_j )$$$.
Adding $$$(l_{max}[r]+1)$$$ (considering $$$0-$$$based indexing) to the answer for each $$$r$$$ gives the desired result.
The overall Time Complexity would be : $$$O(30 N)$$$
For better understanding, refer the implementation below.
#include<bits/stdc++.h>
using namespace std;
//=======================================================================
#define ll long long
#define int long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int,int>
#define vpi vector<pair<int,int>>
#define all(x) (x).begin(),(x).end()
#define countbits(x) __builtin_popcount(x)
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
int mod = 1e9+7;
//int mod = 998244353;
void solve(){
int n, ans = 0;
cin >> n;
vector<int> vec(n);
for (auto &ele : vec){
cin >> ele;
}
//stores rightmost index for each element(1 to 30 inclusive) while iterating for each index
vector<vector<int>> closest;
//stores rightmost index for each element(1 to 30 inclusive) while iterating for last index
vector<int> prevdata(31, -1);
//stores max left pointer(l) for each r.
vector<int> maxl(n, -1);
for (int r = 0; r < n; r++)
{
closest.push_back(prevdata);
//finding maxl for current r by checking all possible Geometric Progressions(size 3) ending at r.
//It could also be the maxl of previous index(i.e (r-1)).
int maxind = -1;
if (r > 1)
{
maxind = maxl[r - 1];
for (int mid = 1; mid < 31; mid++)
{
if ((mid * mid) % vec[r] == 0 && (mid * mid) / vec[r] < 31 && closest[r][mid] != -1)
{
int midind = closest[r][mid];
int first = (mid * mid) / vec[r];
maxind = max(maxind, closest[midind][first]);
}
}
}
//there would be (maxind+1) l,r pairs, so adding this to answer
ans = ans + (maxind + 1);
//storing maxl for r
maxl[r] = maxind;
//updating rightmost index for vec[r]
prevdata[vec[r]] = r;
}
cout << ans << '\n';
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=1;
while(t--){
solve();
}
return 0;
}
Author: JainWinn
Think of Segment Tree.
The problem can be solved by using Segment Tree of multiset. The type $$$-1$$$ operation of adding new items and type $$$-2$$$ operation of cancelling one of the previous type $$$-1$$$ operation or in other words removing item can be thought of as adding and removing elements in multisets in $$$O(log N)$$$ time. Similarly for type $$$-3$$$ operation, the most expensive item $$$A_i$$$ can be found in similar fashion. Overall time taken by multiset over $$$Q$$$ operations/queries is $$$O(log Q)$$$.
The overall Time Complexity would be : $$$O((N+Q) logN logQ)$$$
#include<bits/stdc++.h>
using namespace std;
//=======================================================================
#define ll long long
#define int long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int,int>
#define vpi vector<pair<int,int>>
#define all(x) (x).begin(),(x).end()
#define countbits(x) __builtin_popcount(x)
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
int mod = 1e9+7;
//int mod = 998244353;
class SGTree{
vector<multiset<int>> seg;
public :
SGTree(int n){
seg.resize(4*n+1);
}
void add(int idx,int low,int high,int l,int r,int val){
//no overlap
if(l>high || r<low || low>high){
return;
}
//complete overlap
if(low>=l && high<=r){
seg[idx].insert(val);
return;
}
int mid=low+((high-low)/2);
add(2*idx+1,low,mid,l,r,val);
add(2*idx+2,mid+1,high,l,r,val);
}
void rmv(int idx,int low,int high,int l,int r,int val){
//no overlap
if(l>high || r<low || low>high){
return;
}
//complete overlap
if(low>=l && high<=r){
seg[idx].erase(seg[idx].find(val));
return ;
}
int mid=low+((high-low)/2);
rmv(2*idx+1,low,mid,l,r,val);
rmv(2*idx+2,mid+1,high,l,r,val);
}
void query(int idx,int low,int high,int id,int &ans){
//no overlap
if(id>high || id<low || low>high ){
return;
}
//complete overlap
if(id>=low && id<=high && (!seg[idx].empty())){
ans=max(ans,*(seg[idx].rbegin()));
}
if(low==high){
return;
}
int mid=low+((high-low)/2);
query(2*idx+1,low,mid,id,ans);
query(2*idx+2,mid+1,high,id,ans);
}
};
void solve(){
int n;
cin>>n;
vector<int> a(n,0);
for(int i=0 ; i<n ; i++){
cin>>a[i];
}
SGTree seg(n);
for(int i=0 ; i<n ; i++){
seg.add(0,0,n-1,i,i,a[i]);
}
int q;
cin>>q;
vector<vector<int>> v(q+1);
for(int i=1 ; i<=q ; i++){
int ch;
cin>>ch;
if(ch==1){
int l,r,x;
cin>>l>>r>>x;
l--;
r--;
v[i]={l,r,x};
seg.add(0,0,n-1,l,r,x);
}
else if(ch==2){
int j;
cin>>j;
seg.rmv(0,0,n-1,v[j][0],v[j][1],v[j][2]);
}
else{
int id;
cin>>id;
id--;
int ans=0;
seg.query(0,0,n-1,id,ans);
cout<<ans<<endl;
}
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=1;
while(t--){
solve();
}
return 0;
}



