Problem: 2049B - pspspsps
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):


#include<iostream>

#include<bits/stdc++.h>

using namespace std;

 #define ll long long int 

#define vi vector<int>

#define l_b lower_bound

#define u_b upper_bound

int mod=1e9+7;

void input(vi& arr,int n)

{

    

    for(int i=0;i<n;i++)

    {

        ll j;

        cin>>j;

        arr.push_back(j);

    }

}

    void print(vi& arr,int n)

    {

        for(int i=0;i<n;i++)

        cout<<arr[i]<<" ";

        cout<<endl;

    }

    int upperbound(vector<int>& arr,int low,int high,int target)

    {

         int l=low;

         int h=high;

         //int mid=l+(h-l)/2;

         int ans=-1;

        while(l<=h)

        {int mid=l+(h-l)/2;

            if(arr[mid]<=target)

            {

                ans=mid;

                l=mid+1;

            }

            else

            h=mid-1;

    

        }

        return ans;

    }

    int lowerbound(vector<int>& arr,int low,int high,int target)

    {

         int l=low;

         int h=high;

        

         int ans=high+1;

        while(l<=h)

        {int mid=l+(h-l)/2;

            if(arr[mid]>target)

            {

                ans=mid;

                h=mid-1;

            }

            else

            l=mid+1;

    

        }

        return ans;

    }

   

     

     bool check(vi& arr,ll mid,int n)

     {

        bool res=true;

        if(n%2!=0)

         res=true;

        else

        res=false;

        

        for(int i=0;i<n-1;i+=2)

        {

            if(abs(arr[i]-arr[i+1])<=mid)

            continue;

            else

            if(res)

            {

                res=false;

                i--;

                continue;

            }

            else

            return false;

        }

        return true;

     }

    //   bool   solve(string& s)

    // {

     

    //   int len=s.size();

      

    //       int a;

    //       char b;

    //       cin>>a>>b;

    //       s[a-1]=b;

    //       //cout<<s<<endl;

    //       if(len<4)

    //       return false;

          

    //       for(int i=0;i<len-3;i++)

    //       {

    //         if((check(s,i,i+3)))

    //         return true;

    //       }

    //       return false;

    //   }

       

  int isvalid(vi & arr, int mid,int m)

  {

    int sum=0;

    int n=arr.size();

    for(int i=0;i<n;i++)

    {

        sum+=(mid/arr[i]);

    }

    if(sum==m)

    return 0;

    if(sum<m)

    return -1;

    return 1;

 

  }

   ll solve(int n,vector<ll>& dp)

 

   {  

    if(n<=1)

    return 1;

    if(dp[n]!=-1)

    return dp[n];

    return dp[n]=(solve(n-1,dp)%mod+ ((((n-1)%mod)*2)%mod*(solve(n-2,dp)%mod)))%mod;

      

      }

   int calculateletterrow(vector<vector<char>>& arr,char ch,int row,int m)

   {

       int count=0;

       for(int i=0;i<m;i++)

       {

        if(arr[row][i]==ch)

        {

        count++;

        break;

        }

       }

       return count;

   }

   int calculatelettercolumn(vector<vector<char>>& arr,char ch,int row,int m)

   {

       int count=0;

       for(int i=0;i<m;i++)

       {

        if(arr[i][row]==ch)

        {

        count++;

        break;

        }

       }

       return count;

   }

   bool checkprime(int n)

   {

    for(int i=2;i*i<=n;i++)

    {

        if(n%i==0)

        return false;

    }

    return true;

   }

   void fillvector(vector<ll>& arr)

   {

    arr[0]=1;

    ll count=1;

    for(int i=1;i<=12;i++)

    {

        count*=3;

        arr[i]=count;

    }

   }

   void  solve()

   {

    int n,x;

    cin>>n>>x;

    vi arr;

    input(arr,n);

    vector<ll>dp(x+1);



   dp[0]=1;

    for(int i=0;i<n;i++)

    {

        for(int j=1;j<=x;j++)

        {

           

            if(j-arr[i]>=0)

            dp[j]=(dp[j]+dp[j-arr[i]])%mod;

        }

        

    }

       

        cout<< dp[x]<<endl;

 

    

   }

    int main()

 

    { 

     //   #define int long long int

        ios_base::sync_with_stdio(0);

        cin.tie(0); cout.tie(0);

        int t;

        cin>>t;

       

        while(t--)

        {

          int n;

          cin>>n; 

          string s;

          cin>>s;

         

         int fp=-1;

         int fs=-1;

         int fd=-1;

          for(int i=0;i<n;i++)

          {

              if(s[i]=='p')

              {

              fp=i;

              break;

              }

          }

          int ls=-1;

          bool r=true;

           for(int i=0;i<n;i++)

          {

              if(s[i]=='s')

              {

                  if(r)

              {

              fs=i;

              r=false;

              }

              ls=i;

              }

          }

          bool res=true;

          int count=0;

          int ld=-1;

           for(int i=0;i<n;i++)

          {

              if(s[i]=='.'  )

              {

                  if(res)

                  {

              fd=i;

              res=false;

                  }

                  ld=i;

              }

          }

            if(fp==-1 || fs==-1)

            {

                cout<<"YES"<<endl;

                continue;

            }

            if(fp<ls)

            {

                cout<<"NO"<<endl;

                continue;

            }

            if(fd<fs && fd!=-1)

            {

                if(fd<fs && ld<fs)

                {

                    cout<<"YES"<<endl;

                    continue;

                }

                cout<<"NO"<<endl;

                continue;

            }

            cout<<"YES"<<endl;

        }

    }



Submission (verdict: Accepted):


#include<iostream>

#include<bits/stdc++.h>

using namespace std;

 #define ll long long int 

#define vi vector<int>

#define l_b lower_bound

#define u_b upper_bound

int mod=1e9+7;

void input(vi& arr,int n)

{

    

    for(int i=0;i<n;i++)

    {

        ll j;

        cin>>j;

        arr.push_back(j);

    }

}

    void print(vi& arr,int n)

    {

        for(int i=0;i<n;i++)

        cout<<arr[i]<<" ";

        cout<<endl;

    }

    int upperbound(vector<int>& arr,int low,int high,int target)

    {

         int l=low;

         int h=high;

         //int mid=l+(h-l)/2;

         int ans=-1;

        while(l<=h)

        {int mid=l+(h-l)/2;

            if(arr[mid]<=target)

            {

                ans=mid;

                l=mid+1;

            }

            else

            h=mid-1;

    

        }

        return ans;

    }

    int lowerbound(vector<int>& arr,int low,int high,int target)

    {

         int l=low;

         int h=high;

        

         int ans=high+1;

        while(l<=h)

        {int mid=l+(h-l)/2;

            if(arr[mid]>target)

            {

                ans=mid;

                h=mid-1;

            }

            else

            l=mid+1;

    

        }

        return ans;

    }

   

     

     bool check(vi& arr,ll mid,int n)

     {

        bool res=true;

        if(n%2!=0)

         res=true;

        else

        res=false;

        

        for(int i=0;i<n-1;i+=2)

        {

            if(abs(arr[i]-arr[i+1])<=mid)

            continue;

            else

            if(res)

            {

                res=false;

                i--;

                continue;

            }

            else

            return false;

        }

        return true;

     }

    //   bool   solve(string& s)

    // {

     

    //   int len=s.size();

      

    //       int a;

    //       char b;

    //       cin>>a>>b;

    //       s[a-1]=b;

    //       //cout<<s<<endl;

    //       if(len<4)

    //       return false;

          

    //       for(int i=0;i<len-3;i++)

    //       {

    //         if((check(s,i,i+3)))

    //         return true;

    //       }

    //       return false;

    //   }

       

  int isvalid(vi & arr, int mid,int m)

  {

    int sum=0;

    int n=arr.size();

    for(int i=0;i<n;i++)

    {

        sum+=(mid/arr[i]);

    }

    if(sum==m)

    return 0;

    if(sum<m)

    return -1;

    return 1;

 

  }

   ll solve(int n,vector<ll>& dp)

 

   {  

    if(n<=1)

    return 1;

    if(dp[n]!=-1)

    return dp[n];

    return dp[n]=(solve(n-1,dp)%mod+ ((((n-1)%mod)*2)%mod*(solve(n-2,dp)%mod)))%mod;

      

      }

   int calculateletterrow(vector<vector<char>>& arr,char ch,int row,int m)

   {

       int count=0;

       for(int i=0;i<m;i++)

       {

        if(arr[row][i]==ch)

        {

        count++;

        break;

        }

       }

       return count;

   }

   int calculatelettercolumn(vector<vector<char>>& arr,char ch,int row,int m)

   {

       int count=0;

       for(int i=0;i<m;i++)

       {

        if(arr[i][row]==ch)

        {

        count++;

        break;

        }

       }

       return count;

   }

   bool checkprime(int n)

   {

    for(int i=2;i*i<=n;i++)

    {

        if(n%i==0)

        return false;

    }

    return true;

   }

   void fillvector(vector<ll>& arr)

   {

    arr[0]=1;

    ll count=1;

    for(int i=1;i<=12;i++)

    {

        count*=3;

        arr[i]=count;

    }

   }

   void  solve()

   {

    int n,x;

    cin>>n>>x;

    vi arr;

    input(arr,n);

    vector<ll>dp(x+1);



   dp[0]=1;

    for(int i=0;i<n;i++)

    {

        for(int j=1;j<=x;j++)

        {

           

            if(j-arr[i]>=0)

            dp[j]=(dp[j]+dp[j-arr[i]])%mod;

        }

        

    }

       

        cout<< dp[x]<<endl;

 

    

   }

    int main()

 

    { 

     //   #define int long long int

        ios_base::sync_with_stdio(0);

        cin.tie(0); cout.tie(0);

        int t;

        cin>>t;

       

        while(t--)

        {

          int n;

          cin>>n; 

          string s;

          cin>>s;

         

         int fp=-1;

         int fs=-1;

         int fd=-1;

          for(int i=0;i<n;i++)

          {

              if(s[i]=='p')

              {

              fp=i;

              break;

              }

          }

          int ls=-1;

          bool r=true;

           for(int i=0;i<n;i++)

          {

              if(s[i]=='s')

              {

                  if(r)

              {

              fs=i;

              r=false;

              }

              ls=i;

              }

          }

          bool res=true;

          int count=0;

          int ld=-1;

           for(int i=0;i<n;i++)

          {

              if(s[i]=='.'  )

              {

                  if(res)

                  {

              fd=i;

              res=false;

                  }

                  ld=i;

              }

          }

            if(fp==-1 || fs==-1)

            {

                cout<<"YES"<<endl;

                continue;

            }

           if(ls==0 || fp==n-1)

           {

               cout<<"YES"<<endl;

               continue;

           }

           cout<<"NO"<<endl;

        }

    }