pranay_teja
11/4/2018 - 6:33 PM

CodeChef-AppyLovesOnes

#include<bits/stdc++.h>
using namespace std;

int MaxSub(vector<int> a, int l, int r, int k);
void dec( int n, int &i);

int main(){
    //freopen("ip.txt","r",stdin);
    int n,q,k;
    cin>>n>>q>>k;
    vector<int> a(n);
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    int l=0,r=a.size()-1;
    string x;
    cin>>x;
    map < pair<int,int>, int > memo;
    for(int i=0;i<x.size();i++){
        if(x[i]=='?'){
            if(memo.find({l,r})==memo.end()){
                memo[{l,r}]=MaxSub(a,l,r,k);
            }
            cout<<memo[{l,r}]<<endl;
        }else if(x[i]=='!'){
            dec(a.size(), l);
            dec(a.size(), r);
        }
    }
    return 0;
}
void dec(int n, int &i){
    if(i==0){
        i=n-1;
    }else{
        i--;
    }
}
int MaxSub(vector<int> a, int l, int r, int k){
    int max=INT_MIN;
    int count=0;
    while(true){
        if(count>=k){
            return k;
        }
        if(a[l]==1){
            count++;
        }else if(a[l]==0){
            count=0;
        }
        if(count>max){
            max=count;
        }
        if(l==r){
            break;
        }
        l=(l+1)%a.size();
    }
    if(max>=k){
        max=k;
    }
    return max;
}