SagarPrasad's blog

By SagarPrasad, history, 2 years ago, In English

Question

The above is the link to the problem. I have written a solution using upper_bound function which is getting TLE for 2 out of 13 test cases and barely passing one 1 testcase. Any help in either reducing time in my code or telling the solution will not be accepted with this method will be appreciated. Thank you.(I have put comments in my code to help in understanding.)

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



void solve(){
  int n,m;
  string s;
  cin>>n>>s>>m;

  //Values is like a 2d vector which stores all the occurences of a in val[0], that of b in val[1],etc
  //I also put a value n at the end of all the alphabets to mark a end point.
  //indexes a zero based index
   vector<vector<int>> values(26);
  for(int i=0;i<n;i++){
      values[s[i]-'a'].push_back(i);
  }
  for(int i=0;i<26;i++){
      values[i].push_back(n);
  }
  //In the above two loop i have filled values vector with all the indexes of all the alphabets
  //Note : All the occurences are sorted and there is a end point n present in all the vectors
  //containing the occurencesof each element.


  //I am initially setting my solution as INT_MAX so if i have a solution lower then this by the end
  //of the program i will print that solution(lowest answer) else i will print -1.
  int sol=INT_MAX;

  //We always have to start our subsequences with the letter a so i have decided that i will create
  //a loop with in which i try to find all the answers which start with each occurences of a.
  for(int i=0;i<values[0].size()-1;i++){
      //The temp variable is just to there to check the size of the subsequence will be m.
      int temp=m-1;

      //K is used to identify the position of the alphabet i will be finding next (k%26).
      //K is one since we already have a.
      int k=1;

      //Start is the position of the alphabet a from which we are going to find our answer/subsequence.
      int start=values[0][i];
      //If there are not enough element in the string to find a answer we get out of loop.
      if(start+m-1>=n)
          break;
      //In the beginning we name end=start as the subsequence we have found till now is only till position start
      int end=start;

      //In this loop we will find all m element of the subsequence
      while(temp!=0){
          //D is the alphabet to find.
          int d=k%26;
          //it iterator gives us the alphabet we need to find and we put the position of this alphabet in
          //end.Note we find upper_bound to end as end was the position of our last element
          auto it=upper_bound(values[d].begin(),values[d].end(),end);
          end=values[d][it-values[d].begin()];

          //If end==n we won't able  to complete thi subsequence or any new subsequence so we
          //can finish the program here.
          if(end==n){
              if(sol==INT_MAX){
                  cout<<-1<<"\n";
                  return;
              }
              //SInce we need to print the total numbers to deleted it is sol-m
              cout<<sol-m<<"\n";
              return;
          }
          //If before finding all the element,we find that the current to be solution is
          //greater then actual solution, we can skip this partcular subsequence.
          if(end-start+1>=sol){
              break;
          }
          temp--;
          k++;
      }
      //if end<n i.e there is a soolution
      if(end!=n)
      sol=min(sol,end-start+1);
      //No other soolution is smaller then this so we can end.
      if(sol==m)
          break;
  }
  //This is discussed above.
  if(sol==INT_MAX){
      cout<<-1<<"\n";
      return;
  }
  cout<<sol-m<<"\n";
}
int main(){

#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    freopen("error.txt","w",stderr);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t=1;
    cin>>t;
    while(t--) {
        solve();
    }
    return 0;
}

Code without comments

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



void solve(){
  int n,m;
  string s;
  cin>>n>>s>>m;
   vector<vector<int>> values(26);
  for(int i=0;i<n;i++){
      values[s[i]-'a'].push_back(i);
  }
  for(int i=0;i<26;i++){
      values[i].push_back(n);
  }
  int sol=INT_MAX;
  for(int i=0;i<values[0].size()-1;i++){
      int temp=m-1;
      int k=1;
      int start=values[0][i];
      if(start+m-1>=n)
          break;
      int end=start;
      while(temp!=0){
          int d=k%26;
          auto it=upper_bound(values[d].begin(),values[d].end(),end);
          end=values[d][it-values[d].begin()];
          if(end==n){
              if(sol==INT_MAX){
                  cout<<-1<<"\n";
                  return;
              }
              cout<<sol-m<<"\n";
              return;
          }
          if(end-start+1>=sol){
              break;
          }
          temp--;
          k++;
      }
      if(end!=n)
      sol=min(sol,end-start+1);
      if(sol==m)
          break;
  }
  
  if(sol==INT_MAX){
      cout<<-1<<"\n";
      return;
  }
  cout<<sol-m<<"\n";
}
int main(){

#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    freopen("error.txt","w",stderr);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t=1;
    cin>>t;
    while(t--) {
        solve();
    }
    return 0;
}

Full text and comments »

  • Vote: I like it
  • -28
  • Vote: I do not like it

By SagarPrasad, history, 2 years ago, In English

In the round 753 div 3 , my rank in common standing is being shown 343 whereas in friends standing my rank is 403. Why are there two different ranks? Both the ranks are official ranks.

Full text and comments »

  • Vote: I like it
  • -16
  • Vote: I do not like it

By SagarPrasad, history, 2 years ago, In English
void solve(){
    long long int n;
    cin>>n;
    long long int sol=0;
    for(int i=0;i<63;i++){
        sol=sol+n/pow(2,i);
    }
    cout<<sol<<endl;
}

Here for the test case 1 765228007342234864

solution is not coming correct and I need to typecast my pow(2,i) to (long long int)pow(2,i). For i=0, n/pow(2,i) is not even equal to n, why is it so?

As far as i know pow function gives value in double but even so the value of double should be 1.0 and hence n/(1.0) should be n, instead i am getting 765228007342234880 for the above test case which is more then n. 1362C - Джонни и ещё одно падение рейтинга Any help is appreciated. Thank You

Full text and comments »

Tags pow, c++
  • Vote: I like it
  • -16
  • Vote: I do not like it

By SagarPrasad, history, 3 years ago, In English

I was doing a question Advertising Agency Your text to link here... where i need to find NcR but if N is very large i had to use modulo of 10^9+7 but for some reason my answer gets wrong on test 6 where n is very big. I think the problem is not using modulo properly but i cant figure it out. 1475E][SUBMISSION:121841381 - Advertising Agency any help is appreciated. In my code n=to and r=bef;

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

//#include <chrono>
//using namespace std::chrono;

long long multi(long long a, long long b, long long mod){
    return (a * b) % mod;
}

long long power(long long a, long long b, long long mod){
    long long powans = 1;

    for(; b > 0; a = multi(a, a, mod), b /= 2) if(b % 2 == 1) powans = multi(powans, a, mod);

    return powans;
}

void fastscan(int &x)
{
    bool neg=false;
    register int c;
    x =0;
    c=getchar();
    if(c=='-')
    {
        neg = true;
        c=getchar();
    }
    for(;(c>47 && c<58);c=getchar())
        x = (x<<1) + (x<<3) +c -48;
    if(neg)
        x *=-1;
}

int gcd(long long int a,long long int b){
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
long long int fact(long long int to,long long int bef){
    long long sol=1;
    if(to-bef<bef)
        bef=to-bef;
    long long int p=bef;
    long long int n=1,d=1;
    while(p--){
        n=n*to;
        d=d*bef;
        long long int m=gcd(n,d);
        n=n/m;
        d=d/m;
        n=n%1000000007;
        d=d%1000000007;
        to--;
        bef--;
    }
    return n;
}

void solve(){
    int n,k;
    cin>>n>>k;
    int arr[n];
    for(int i=0;i<n;i++)
        cin>>arr[i];
    sort(arr,arr+n,greater<int>());
    int value=arr[k-1];
    long long int bef=0,to=0;
    for(int i=0;i<n;i++){
        if(arr[i]==value){
            to++;
            if(i<=k-1)
                bef++;
        }
    }
    long long int sol=1;
    sol=fact(to,bef);
    cout<<sol%1000000007<<endl;
}
int main(){

#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    freopen("error.txt","w",stderr);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //auto start = high_resolution_clock::now();
    int t;
    cin>>t;
    while(t--) {
        solve();
    }
    // auto stop = high_resolution_clock::now();
    // auto duration = duration_cast<microseconds>(stop - start);
    // cout << duration.count() << endl;
    return 0;
}

Full text and comments »

  • Vote: I like it
  • -3
  • Vote: I do not like it