645C - Enduring Exodus - CodeForces Solution


binary search two pointers *1600

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define fast_cin() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
// long long int n,m,k,flag=0,sz=0,v[51][51],dx[]={0,0,0,1},dy[]={1,0,0,0};
// string s[51];
// vector<pair<long long int,pair<long long int,long long int>>>vv;
// void dfschng(long long int x,long long int y)
// {
//  if(x<0 || y<0 || x>=n || y>=m)
//  {
//   return;
//  }
//  if(v[x][y] || s[x][y]=='*')
//  {
//   return;
//  }
//  //cout<<x<<" "<<y<<"\n";
//  v[x][y]=1;s[x][y]='*';
//  for(long long int i=0;i<4;i++)
//  {
//   long long int xx=x+dx[i],yy=y+dy[i];
//   dfschng(xx,yy);
//  }
// }
// void dfs(long long int x,long long int y)
// {
//  if(x<0 || y<0 || x>=n || y>=m)
//  {
//   return;
//  }
//  if(v[x][y] || s[x][y]=='*')
//  {
//   return;
//  }
//  v[x][y]=1;sz++;
//  if(x==0 || y==0 || x==(n0) || y==(m0))
//  {
//   flag=1;
//  }
//  for(long long int i=0;i<4;i++)
//  {
//   long long int xx=x+dx[i],yy=y+dy[i];
//   dfs(xx,yy);
//  }
// }
// long long int n;
//  cin>>n;
//  for(long long int i=1;i<=n;i+=2)
//  {
//   cout<<i<<" ";
//  }
//  for(long long int i=n-(n&1);i>=1;i-=2)
//  {
//   cout<<i<<" ";
//  }
//  cout<<"\n";
long long int calc(vector<long long int>&v,string s,long long int idx,long long int k)
{
 long long int n,op;
 n=v.size()-1;
 for(long long int i=1;i<=n;i++)
 {
  if(s[i]=='0')
  {
   op=v[min(n,i+idx)]-v[max(1ll,i-idx)-1];
   if(op>k)
   {
    return 1;
   }
  }
 }
 return 0;
}
void solve()
{
 long long int n,k,l,r,m,op=0;
 cin>>n>>k;
 string s;
 cin>>s;
 s='.'+s;
 vector<long long int>v(n+1,0);
 for(long long int i=1;i<=n;i++)
 {
  v[i]=v[i-1]+(s[i]=='0');
 }
 l=0;r=n;
 while(r-l>1)
 {
  m=(l+r)/2;
  if(calc(v,s,m,k))
  {
   r=m;
   op=r;
  }
  else
  {
   l=m;
  }
 }
 cout<<r;
}
int main()
{
    fast_cin();
    long long int t;
    //cin>>t;
    t=1;
    while(t--)
    {
     solve();
    }
   return 0;
}


Comments

Submit
0 Comments
More Questions

101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake